Why can't I add a string to my NSMutableArray [duplicate] - ios

This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 7 years ago.
I have created a function within my code that converts whatever is in the textfield of my app to append to my array. However, when I set a breakpoint in the code, it returns that my array, _descriptionArray is nil. Why is that?
I want whatever value is in the descriptionTextField.text to append to my _descriptionArray.
Thanks for the help!
-(NSMutableArray *)descriptionConversion{
[_descriptionArray addObject: (descriptionTextField.text)];
return _descriptionArray;
}

Did you ever initialize _descriptionArray? If not, be sure to initialize the array with _descriptionArray = [NSMutableArray array];. If you don't initialize the array, it will be nil. You can only add an object to an array; not to nil.

Related

how to fix initializing nsmutable array strong with an expression of incompatible type void warning?

I am using this code NSMutableArray * nst = [navigationArray removeObjectAtIndex:navigationArray.count-1]; to remove some object in a nsmutablearray but i am getting "nsmutable array strong with an expression of incompatible type void warning" can any one help how to fix it.
You should do something like this
NSMutableArray * nst = [NSMutableArray arrayWithArray:navigationArray ];
[nst removeObjectAtIndex:navigationArray.count-1];
NSLog(#"%#",nst);
This way, navigation array stays intact as it was, and nst is a new array with the navigation array content and the objects removed at the specific index.
OR
If you want to remove those contents straight away from navigationArray, then
[navigationArray removeObjectAtIndex:navigationArray.count-1]
This will make the objects removed from the navigationArray itself.
Xcode is complaining coz, [removeObjectAtIndex] doesn't returns an array and you are trying to assign nst to the return value of the method.
Hope could make u understand.

Passing Null Values and a variable in to NSMutableArray/NSArray. Why is there a difference in output?

My question is how to pass Null values in to NSMutableArray/NSArray.
But by searching google and going through different sites, i think that i have achieved my purpose. But, while trying i got a new doubt.
See the below code to understand my question
I am declaring a mutable array and initializing it with some objects initially and also i am passing a null in to it at the time of declaration itself.
Now when i run this code below
NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:#"first",#"last", nil];
[ar addObject:[NSNull null]];
NSLog(#"%#",mar);
The o/p is :
(
first,
last,
"<null>"
)
But my actual doubt is when i initialize the NSMutableArray as below
NSString *str;
NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:#"first",str,#"last", nil];
[ar addObject:[NSNull null]];
NSLog(#"%#",mar);
The o/p is :
(
first,
"<null>"
)
The 2nd element in the array is "null", i understand that as i have passed a variable without assigning any value to it. It is printing null. But why are the remaining elements not printed.
According to what i know, the array will stop adding elements whenever it overcomes nil while initializing. But here, in this case. The o/p shows that the 2nd element is also null.(But not nil.)
Then why does the remaining elements are not printed.
UPDATING QUESTION
NSString* str;
NSLog(#"%#",str);
NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:#"first",str,#"last", nil];
NSLog(#"%#",mar);
and the o/p for the above code is
(null) // for str an uninitialised NSString variable.
(
first // first element in the array
)
If the value present in str is (null), how come the array encountered nil and stopped adding elements to array and printing them.
Now, someone answer this?
initWithObjects uses nil as the marker for the last item. If you pass a nil (the unitialized str variable) to it as the 2nd item out of 4, only the first one is actually added.
[[NSMutableArray alloc]initWithObjects:#"first",str ?: [NSNull null],#"last", nil];
will fix your problem by replacing the nil with a [NSNull null].
Also see NSArray creation with variable argument lists
in objective-C, when iterating through Array if nil/null encountered then it gets treated as end of array.
this is the reason it is not printing remaining values.
in case you want clear diff between nil/null/Nil then check Difference between nil, NIL and, null in Objective-C

NSMutableArray Extra Nil Sentinels

I've read a text file into an array of strings. In the below code I'm creating objects from that array and adding them into an NSMutableArray.
NSMutableArray* metaphors = [[NSMutableArray alloc] init];
unsigned int i, cnt = [allLinedStrings count];
for(i = 0; i < cnt-3; i+=5)
{
Metaphor *newMetaphor = [[Metaphor alloc] init];
[newMetaphor setMetaphorTitle: allLinedStrings[i]];
[newMetaphor setCorrectAnswer: allLinedStrings[i+1]];
[newMetaphor setLiteralAnswer: allLinedStrings[i+2]];
[newMetaphor setWayOffAnswer: allLinedStrings[i+3]];
[metaphors addObject:newMetaphor];
}
As for the problem, when I access any item via index ([metaphors objectAtIndex:3] for example) every other element (odd numbered ones) are nil elements. All of the objects are added to the array, though. My guess is that addObject is adding an element to the array as well as a new nil sentinel every time? Should this be happening/should I manually go through and remove these elements?
Also a side note, as I'm new to Objective-C, my Metaphor class contains the 4 instance fields you can see within the body: I'm sure there is quicker syntax to initialize one of these objects if anyone could point me the right way. Thanks.
NSArray can't contain nil. It's invalid. If you are getting nil back in a call to an array, the array pointer itself is almost certainly nil. (You CAN send messages to a nil object pointer in Objective C. It simply returns nil/zero.)
Trying to add a nil to an NSArray will cause a crash, and trying to index past the end of an NSArray will also crash.
There is a special class NSNull that provides a singleton placeholder object that can take the place of a nil entry in an NSArray.
The nil sentinel is only a way to know the last element of a variable length array has been reached. The nil sentinel ISN'T added to the NSMutableArray;
NSMutableArray addObject method doesn't need a nil sentinel as you only add ONE object, not a variable length array.
If you need to add something "nil" to an array, you might use [NSNull null] which is an object "equivalent to nil".

Need to check if NSArray object contains specific text. [duplicate]

This question already has answers here:
Finding a substring in a NSString object
(7 answers)
NSString search whole text for another string
(3 answers)
Closed 9 years ago.
I am performing a method call that places any objects found in the background into an NSArray object called "objects".
When I NSLog the counts property of "objects" it tells me that the array contains 1 object which is correct.
When I NSLog the NSArray object called "objects" it prints out the following:
(
"<PFUser:nzyjeVFgjU:(null)> {\n email = \"hahshshs#aol.com\";\n username = hzhjsshhsppppp;\n verificationCode = 6449;\n}"
)
Here is my problem. I need to create an if statement that takes another value I already have and compares it to the 4 digit number that verificationCode is equal to eg. Above in the code it says "verificationCode = 6449"
I am basically trying to compare a 4 digit code that I already have to the verificationCode that is contained in this single NSArray object.
I know how to write if statements, but i have no idea how to specifically focus on "verificationCode = 6449" since it is just text inside of a string.
I have been trying to figure out a way to do this for an hour now so any help is greatly appreciated thank you.
Just found the answer here: https://stackoverflow.com/a/7574136/3117509
I tried searching earlier but was searching for "search array for string" when I should have been searching for something like "search for string within a string" or "search for text within a string."
If I understand your question correctly, you're trying to iterate through an array and find if a string is there? Don't have access to a Mac right now, so I'm not sure if this will compile.
NSArray* someArray = [[NSArray alloc] initWithObjects:#"test", #"test1", #"test2", nil];
for(int i = 0; i < (int)[someArray count]; i++) {
if([[someArray objectAtIndex:i] isEqualToString #"test"]) {
//match found. handle the match
}
}

How to extract an Array out of NSArray? [duplicate]

This question already has answers here:
NSArray of united Arrays
(6 answers)
Closed 8 years ago.
I have been using an NSDictionary to store my JSON response. Now I tried to store them inside an array, so I can iterate over each values.
I used: valueForKeyPath and now each of those values are inside an NSArray.
The problem is instead of returning self.myarray.count (3), I only get (1). It wrote
the NSDictionary Keys into myarray at Index 0. So I have an Array inside myarray at Index 0 containing values (2,5,3).
Is there a way to quickly fix this and putting this like: index 0 : 2 , index 1: 5 , index 2: 3
and not index 0 : (2,5,3)?
Thanks I am still learning Obj-C.
As I understand you have perform something like following: [self.myarray addObject: dict.keys];
Use following instead: self.myarray = dict.keys;
If you do this only because you want to iterate over each value you could do the same with dictionary: for(id key in dict) or for(id key in dict.keys) iterates over each key. for(id value in dict.values) iterates over each value.

Resources