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];
}
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];
This code:
server_response = [{id:1},{id:2},{id:3},{id:4}]
I am getting above response from server now I want only the list of ids in one array like
ids = [1,2,3,4];
I know we can do by for loop but it takes long time if thousand of ids inside the response array.
Is there any better way to achieve above equation?
NSArray *result = [yourArray valueForKey:#"id"]
From the documentation for NSArray instance method valueForKey:
Returns an array containing the results of invoking valueForKey: using key on each of the array's objects
NSMutableArray *resultArray = [NSMutableArray array];
for (NSDictionary *dict in server_response) {
[resultArray addObject:[dict objectForKey:#"id"]];
}
Try above code. Hope it will help you. Result array has final values
NSArray *server_response = #[#{#"id":#"1"},#{#"id":#"2"},#{#"id":#"3"},#{#"id":#"4"}];
NSMutableArray *resultArray = [NSMutableArray array];
NSString *birdtemp;
for (NSDictionary *object in server_response) {
birdtemp = object[#"id"];
[resultArray addObject:birdtemp];
}
NSLog(#"%#",resultArray);
OutPut: [
1,
2,
3,
4
]
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];
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];
}
NSMutableDictionary *expense_ArrContents = [[NSMutableDictionary alloc]init];
for (int i = 1; i<=4; i++) {
NSMutableArray *current_row = [NSMutableArray arrayWithObjects:#"payer_id",#"Expense_Type_id",#"Category_Id",#"SubCategory_Id",nil];
[expense_ArrContents setObject:current_row forKey: [NSNumber numberWithInt:i]];
}
NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"];
NSLog(#"%#",[newArray description]);
i want to get the list of key values containing the particular object which is in the array of values stored in nsmutabledictionary for a particular key.
In the line where you get all the keys ([expense_ArrContents allKeysForObject:#"payer_id"];) you actually get keys for an object that is not in any of the array's items. This #"player_id" is different object than the #"player_id" you added in current_row. In fact, maybe all of your rows have different #"player_id" objects (except if the compiler has made some optimization - maybe it threats that same string literal as one object instead of creating new object for each iteration).
Try creating an NSString object for the #"player_id" which you add to the current_row and then get all the keys for that same object:
NSString* playerId = #"player_id";
for(){
NSMutableArray *current_row = [NSMutableArray arrayWithObjects: playerId,...];
...
}
NSArray *newArray = [expense_ArrContents allKeysForObject:playerId];
Your NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"]; will not return any value because in expense_ArrContents there is no such key(#"payer_id"), instead there are keys like 1,2,3 etc.What is your requirement?Want to see what all keys are there in expense_ArrContents just log
NSArray*keys=[expense_ArrContents allKeys];
Try this :
NSMutableArray *array_key=[[NSMutableArray alloc]init];
for (NSString *key in expense_ArrContents) {
if ([[expense_ArrContents objectForKey:key] containsObject:#"payer_id"]) {
[array_key addObject:key];
}
}