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];
Related
I have the following situation where
I have an NSMutableArray filled with an xml file which I want to search.
When I enter something in the search field I get this Error:
-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x5b388b0
What does it means and how can I fix it??
I suppose the error is somewhere around here.
- (void)searchTableView{
searchedList = [[NSMutableArray alloc] init];
NSLog(#"new list %#", searchedList);
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in list) {
NSArray *array = [dictionary objectForKey:#"TITLE"];
[searchArray addObjectsFromArray:array];
}
for (NSString *TempArray in searchArray) {
NSRange titleResults = [TempArray rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResults.length > 0)
[searchedList addObject:TempArray];
}
[searchArray release];
searchArray = nil;
}
it means you are calling a method designed for an NSArray (countByEnumeratingWithState:objects:count on a NSString.
I don't know ifthis code is copy/paste from yours, but if so, at the end where you use [searchList addObject:TempArray] you don't have an object named searchList.
Also, work on your naming conventions. big time.
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 developing one iPad application.I have one NSMutableArray and NSMutableDictionary .These both are changeable based on the data from the web service.I need to remove some dictionary from my NSMutableArray based on the NSMutableDictionary values. Here I explain the situation through one example:
testArray =[{ language :"ESP"},{language :"ENG"},{language :"ENG"},{language :"FRH"}];
From the test array i need to remove the all Dictionaries which have key value language :"ENG".
I've written code like this:
for(int i =0;i<testArray.count;i++){
NSString *lang = [NSString stringWithFormat:#"%#", [testArray[i] objectForKey:#"language"]];
if([lang isEqualToString:#"ENG"]){
[testArray removeObjectAtIndex:i];
}
}
But it is not working. I think the problem is when I remove the dictionary from at index the array count is also reducing so the loop is executing based on new array count. Some help me to rewrite the code for get exact answer?
This is my favorite way, it's fast, clear and correct.
NSMutableArray *itemsToRemove = [NSMutableArray array];
for (id item in theArray) {
if ([item shouldBeRemoved])// Condition to check the key pair Value
[itemsToRemove addObject:item];
}
[theArray removeObjectsInArray:itemsToRemove];
Try this.
NSMutableArray *arrTemp = [[NSMutableArray alloc]initwithArray:testArray];
for(int i =0;i<testArray.count;i++){
NSString *lang = [NSString stringWithFormat:#"%#", [testArray[i] objectForKey:#"language"]];
if([lang isEqualToString:#"ENG"]){
[arrTemp removeObjectAtIndex:i];
}
}
[testArray removeAllObjects];
testArray = arrTemp;
for(int i =0;i<testArray.count;i++){
NSString *lang = [NSString stringWithFormat:#"%#", [testArray[i] objectForKey:#"language"]];
if([lang isEqualToString:#"ENG"]){
[testArray removeObjectAtIndex:i];
i--;
}
}
Replace your code with below code.
NSMutableArray *arrTemp = [NSMutableArray new];
for(int i =0;i<testArray.count;i++){
NSString *lang = [NSString stringWithFormat:#"%#", [testArray[i] objectForKey:#"language"]];
if([lang isEqualToString:#"ENG"]){
[arrTemp addObject:[NSNumber numberWithInt:i]];
}
}
for(int k=0;k<[arrTemp count];k++)
{
int ii = [[arrTemp objectAtIndex:k]intValue];
[testArray removeObjectAtIndex:ii];
}
let me know it is working or not!!!
Happy Coding!!!
I would implement that using NSPredicate:
NSMutableArray *testArray = [#[#{ #"language" :#"ESP"}, #{#"language" :#"ENG"},
#{#"language" :#"ENG"}, #{#"language" :#"FRH"}] mutableCopy];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"language != %#", #"ENG" ];
testArray = [[testArray filteredArrayUsingPredicate:predicate] mutableCopy];
I have just tested it, it works (it is short and nice to read, but NSPredicate can be really slow).
Another way to do it is using enumerateObjectsWithOptions:usingBlock:
[testArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSDictionary *dict, NSUInteger index, BOOL *stop) {
if ([dict[#"language"] isEqualToString:#"ENG"]) {
[testArray removeObjectAtIndex:index];
}
}];
Pleace notice that i use NSEnumerationReverse as NSEnumerationOptions because according to docs of removeObjectAtIndex:- Method :
To fill the gap, all elements beyond index are moved by subtracting 1
from their index.
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 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];
}