i have two nsmutableArray . Arr_jsondata and second is tempArray.
Already Arr_jsondata contain the data from json link and display in tableview .
i want to add the tempArray data into Arr_jsondata and reload the table using Arr_jsondata , because tableview delegate is also used this array to display the data in tableview .
but its always give me an error when i add temp array data into arr_jsondata .
for (int i=0; i<[Temp_arr_JsonData count]; i++)
{
NSString *str_brandname = [[Temp_arr_JsonData objectAtIndex:i] valueForKey:#"storebrand"];
NSLog(#"%#",str_brandname);
NSObject *myNewObject = [[NSObject alloc] init];
if ([str_brandname isEqualToString:#"Nike"])
{
NSLog(#"Data matched");
c++;
myNewObject = [Temp_arr_JsonData objectAtIndex:i];
[temparray addObject:myNewObject];
}
}
// arr_JsonData=temparray;
[[arr_JsonData arrayByAddingObjectsFromArray:temparray] mutableCopy];
NSLog(#"%#",temparray);//display all nike related data
NSLog(#"%#",arr_JsonData);//display null array .
[self.tableView reloadData];
[sender setSelected:YES];
in last i want only tempdata into Arr_jsondata ..... what can i do ?
Just add objects from array. See this apple's doc
if (arr_JsonData.count > 0)
[arr_JsonData addObjectsFromArray: (NSArray*)temparray];
else
arr_JsonData = [NSMutableArray arrayWithArray:(NSArray*)temparray]
You can try this
[arr_JsonData addObjectsFromArray:tempArray];
I think arr_JsonData is not NSMutableArray actually which can be judged from the exception description you listed in the answer. Try this:
arr_JsonData = [NSMutableArray arrayWithArray:arr_JsonData];
[arr_JsonData addObjectsFromArray: temparray];
Try using this:
NSMutableArray *ar = [NSMutableArray alloc] initWithArray:ar1];
Just have to initialize array with other array and it will have all contents from other array.
Or this one:
NSMutableArray *ar = [NSMutableArray alloc] initWithArray:ar1 copyItems:YES];
mutableCopy is defined for NSArray not for NSMutableArray
if ([arr_JsonData count] > 0)
{
[arr_JsonData addObjectsFromArray: temparray];
}
Related
i am trying to show selected cell checkmark when user is offline,but the array is not adding another array object, kindly help me
1.appDelegate.SelectedIDArray saving selected cell
2.buildingObject.selectedIDString saving checked cell index coma separated
//first i am removing all objects from array
[appDelegate.SelectedIDArray removeAllObjects];
//then i am adding string values in array(e.g 3,2,7)
NSMutableArray *tempArray = (NSMutableArray*)[buildingObject.selectedIDString componentsSeparatedByString:#","];
//now adding tempArray array objects in appDelegate.SelectedIDArray
[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];
//now showing count of added object in appDelegate.SelectedIDArray
txtID.text=[NSString stringWithFormat:#"%zd of 23 selected",appDelegate.SelectedIDArray.count];
by saying offline, you mean app is terminated, then if you didn't save your array in NSUserDefaults it is nil already. then you should initialize it. if this is not the case another problem may be with appDelegate:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *vals = #"1,2,3,4,5,6,7,8";
NSMutableArray *tempArray = [[vals componentsSeparatedByString:#","] mutableCopy];
if (!appDelegate.SelectedIDArray) {
appDelegate.SelectedIDArray = [NSMutableArray new];
}
else
{
[appDelegate.SelectedIDArray removeAllObjects];
}
[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];
NSLog(#"%#", appDelegate.SelectedIDArray);
I've took sample test case similar to your code:
NSString *str1 = #"1,2,3,4,5,6,7,8,9";
NSMutableArray *tempArray = (NSMutableArray*)[str1 componentsSeparatedByString:#","];
NSMutableArray *arr = [NSMutableArray array];
[arr addObjectsFromArray:tempArray];
NSLog(#"%#",arr);
Its working fine for me. Make sure appDelegate.SelectedIDArray is NSMutableArray. Or if you want fresh array simply use
appDelegate.SelectedIDArray = [NSMutableArray arraywitharray:tempArr];
Hope this helps.
I hope you did not forget to alloc appDelegate.SelectedIDArray.
Hello buddy please try this
NSArray *tempArray = [buildingObject.selectedIDString componentsSeparatedByString:#","];
[appDelegate setSelectedIDArray:[tempArray mutableCopy]];
if it didnot work then possibly one of your array is empty.Please check
use it, it will fix if you are using the non arc
appDelegate.SelectedIDArray = [tempArray retain];
I have a simple NSMutableArray which I am trying to store a few objects in. However in NSLog, the contents of the array always comes as null... I just dont understand why. Here is my code:
In my header file:
NSMutableArray *dataFiles;
In viewDidLoad:
dataFiles = [[NSMutableArray alloc] init];
Later on in my code in a method which is trying to add a string to my NSMutableArray:
[dataFiles insertObject:url atIndex:0]; // 'url' is an an NSURL.
What am I doing wrong? This is always how I have used NSMutableArray's, why are they all of a sudden not working?
UPDATE
I did indeed do an NSLog on the "url" (NSURL) before its being added to the array and it is not null at all. Here is the output:
THE URL: file:///var/mobile/Containers/Data/Application/E991FAFC-80DB-437B-B214-96720B1AA7AF/Documents/19Feb15_072308am.aif
UPDATE 2
I just tried #Dheeraj Singh solution and it did not work:
if ([dataFiles count] == 0) {
[dataFiles addObject:url];
}
else {
[dataFiles insertObject:url atIndex:0];
}
NSLog(#"data in: %#", dataFiles);
Thanks for your time, Dan.
Not sure what is wrong, but below (your) code is working fine with me.
NSMutableArray * arr = [[NSMutableArray alloc]init];
NSString *murl = #"file:///var/mobile/Containers/Data/Application/E991FAFC-80DB-437B-B214-96720B1AA7AF/Documents/19Feb15_072308am.aif";
NSURL *url = [NSURL URLWithString:murl];
[arr insertObject:url atIndex:0];
NSLog(#"Array is %#",arr);
Output
Array is (
"file:///var/mobile/Containers/Data/Application/E991FAFC-80DB-437B-B214-96720B1AA7AF/Documents/19Feb15_072308am.aif"
)
What I strongly feel is you are using NSArray against NSMutableArray. Please confirm the same.
Could you post the actual code so that we can tell you what is going on?
Ok after a bit of playing around I found out what was "wrong" or at least what is stopping my code from working. It is because before I was initialising the NSMutableArray in the viewDidLoad method. As soon as I moved the NSMutableArray initialisation code to method where I wanted to write the data to it, it started working. Anyone know why?? Here is my code now:
// Initialise the audio arrays.
// Originally this line was in the viewDidLoad.
dataFiles = [[NSMutableArray alloc] init];
if ([dataFiles count] == 0) {
[dataFiles addObject:url];
}
else {
[dataFiles insertObject:url atIndex:0];
}
You can do in Following way :
NSMutableArray * arr = [[NSMutableArray alloc]init];
NSString *url = #"www.test.com";
[arr addObject:url];
NSLog(#"Count of Array is %i",[arr count]);
*** if you want to add multiple items then you can do by following way
for (int i =0; i < 5; i++) {
[arr addObject:#"Hello"];
}
NSLog(#"Count of array is %i",[arr count]);
I am working with an application in which i am getting photoID ,which is a string.
I am storing that photoID in array,and again add that array in another array.
Below iS the code::
NSString *photoID;
arr=[[NSMutableArray alloc]initWithCapacity:10];
array=[[NSMutableArray alloc] init];
[array addObject:photoID];
[arr arrayByAddingObjectsFromArray:array];
//number=(int)arr[1];
NSLog(#"arr : %#",arr);
NSLog(#"arr[0] : %#",arr[0]);
NSLog(#"arr[1] : %#",arr[1]);
NSLog(#"Number1 : %#",number1);
NSLog(#"Number : %d",number);
when i tried to access the value of arr[1],my application crashes.
i don't know what am i doing wrong.am i doing wrong to add strings in array,and truing to access unsaved data?
Please help me out.
Thanks in advance
It is because this line: [arr arrayByAddingObjectsFromArray:array]; does nothing to the arr, it only
Returns a new array that is a copy of the receiving array with the
objects contained in another array added to the end.
You should replace it with [arr addObjectsFromArray:array];. And also, you only have 1 element in arr which is at index 0, so the arr[1] should crash but arr[0] should work.
First array should be NSArray if you want to arrayByAddingObjectsFromArray or addObjectsFromArray
NSArray *array1=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableArray *array2=[[NSMutableArray alloc]init];
[array2 addObjectsFromArray:array1];
NSLog(#"%d",array2.count);
You can also use like this:
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3", nil];
NSMutableArray *outerArray = [NSMutableArray array];
for(int i=0;i<=innerArray.count;i++)
{
[outerArray addObject:innerArray];
}
I am in my IOS application in which i am getting ID from server which i am saving in string and then add strings in NSMutableArray.I am not getting perfect method by which i can add the strings in array and use the array outside the scope.
Here is my code Please help me out::
- (void)flickrAPIRequest:(OFFlickrAPIRequest *)inRequest didCompleteWithResponse:(NSDictionary *)inResponseDictionary
{
NSMutableArray *array=[[NSMutableArray alloc]init];
i=0;
NSLog(#"%s %# %#", __PRETTY_FUNCTION__, inRequest.sessionInfo, inResponseDictionary);
if (inRequest.sessionInfo == kUploadImageStep)
{
snapPictureDescriptionLabel.text = #"Setting properties...";
NSLog(#"%#", inResponseDictionary);
NSString* photoID =[[inResponseDictionary valueForKeyPath:#"photoid"] textContent];
flickrRequest.sessionInfo = kSetImagePropertiesStep;
// for uploading pics on flickr we call this method
[flickrRequest callAPIMethodWithPOST:#"flickr.photos.setMeta" arguments:[NSDictionary dictionaryWithObjectsAndKeys:photoID, #"photo_id", #"PicBackMan", #"title", #"Uploaded from my iPhone/iPod Touch", #"description", nil]];
[self.array addObject:photoID];
arr=array[0];
counterflicker++;
NSLog(#" Count : %lu", (unsigned long)[array count]);
}
How can i add the photoID(Strings) in the array?
Please help me out..
for adding NSString in NSMutableArray is like this
NSString *str = #"object";
NSMutableArray *loArr = [[NSMutableArray alloc] init];
[loArr addObject:str];
In your code Why are you using self.array ? just write like this. [array addObject:photoID];
self keyword is used for global variables but here in your code
"array" is a local variable .So need of self.array
[array addObject:photoID];
Before adding check that photoID is nil or not
if (photoID.length > 0) {
[array addObject:photoID];
}
I observe that in your code. you declare mutable array in local scope.
So just use
[array addObject:photoID];
Instead of
[self.array addObject:photoID];
May be you are create property for this array with same name, then you need to alloc it.
If you create a property for this then remove local declaration and alloc array like this.
self.array=[[NSMutableArray alloc]init];
and then use
[self.array addObject:photoID];
On touch up inside of SAVE button, the following code is executed:
- (IBAction)onSave:(id)sender {
savecount++;
[self saveNumberOfContacts];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:nameTextField.text];
[myArray addObject:phoneTextField.text];
[myArray addObject:addressTextField.text];
[myArray addObject:cityTextField.text];
[myArray addObject:stateTextField.text];
[myArray addObject:zipcodeTextField.text];
[myArray writeToFile:[self saveFilePath] atomically:YES];
}
This creates a single array. I want to know how to dynamically create multiple arrays with the savecount variable suffixed at end of the array name.
For example, if my savecount is 3, then myArray1, myArray2, myArray3 should be created.
P.S. savecount changes its value dynamically.
EDIT: i dont want this method creating a number of arrays every time i call it. See, the user's info is stored in myArray1 when i click save for the first time. Now, the savecount gets increemented(say,savecount=2). When i enter another user's details and click save, i dont want myArray1 to be overwritten or disturbed; the second user's details must be independently saved in myArray2.
NSMutableArray *holder;
- (void)viewDidLoad
{
holder = [[NSMutableArray alloc] init];
}
- (IBAction)onSave:(id)sender {
savecount++;
[self saveNumberOfContacts];
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:nameTextField.text];
[myArray addObject:phoneTextField.text];
[myArray addObject:addressTextField.text];
[myArray addObject:cityTextField.text];
[myArray addObject:stateTextField.text];
[myArray addObject:zipcodeTextField.text];
[myArray writeToFile:[self saveFilePath] atomically:YES];
[holder addObject:myArray];
}
Have one global array in which you can hold dynamically created array. Now when you get value from holder, you will have separated myArray object.
If you are only using these arrays within one method, you could instead store the arrays in an array. This doesn't give you array names such as myArray1, myArray2, etc, but accomplishes the same overall task. Here's an example:
NSMutableArray *myArrays = [[NSMutableArray alloc] initWithCapacity:savecount];
for (NSUInteger i = 0; i < savecount; i++) {
NSMutableArray *newArray = [[NSMutableArray alloc] init];
[myArrays addObject:newArray];
[newArray addObject:nameTextField.text];
// continue adding your objects to newArray
}
Now you can reference your arrays as [myArrays objectAtIndex:0]...[myArrays objectAtIndex:savecount-1].
I think your problem is the path of save, or same path with appended array data.
If save data to different path:
- (NSString*)saveFilePath{
return [NSString stringWithFormat:#"%#/%#%d.%#", thePathToSave, fileName, savecount, fileType];
}
If save to same path with appended array data:
Replace:
[myArray writeToFile:[self saveFilePath] atomically:YES];
with
NSArray *fileData = [NSArray arrayWithContentsOfFile:[self saveFilePath]];
NSMutableArray *preDatas = nil;
if (preDatas.count == 0) {
preDatas = [NSMutableArray array];
}
else{
preDatas = [NSMutableArray arrayWithArray:fileData];
}
[preDatas addObject:myArray];
[preDatas writeToFile:[self saveFilePath] atomically:YES];