Simple NSInteger and NSMutableArray question - objective-c

I'm trying to access an array using another array integer value as an index.
NSInteger index=[appDelegate.randomRiddles objectAtIndex:appDelegate.randomRiddlesCounter];
int i = index;
questionText.text = [[appDelegate.currentRiddlesContent objectAtIndex:i] objectForKey:#"question"];
//where appDelegate.randomRiddlesCounter is an NSInteger and appDelegate.randomRiddles is a NSMutableArray
However I'm getting incompatible pointer to int conversion warning. How can I fix this above code? The warning I get is coming from the first line.

Try:
NSNumber *index = [appDelegate.randomRiddles objectAtIndex: appDelegate.randomRiddlesCounter];
int i = [index intValue];
questionText.text = [[appDelegate.currentRiddlesContent objectAtIndex: i] objectForKey: #"question"];
NSInteger is an integral type, not an object.

Try this:
int i = [index intValue];

An NSArray like object can only store Objective-C object pointers (i.e. everything that you can assign to an id)
With objectAtIndex you get the object, with indexOfObject:(id)anObject you get the corresponding index.
These two instructions are both valid:
id bla = [appDelegate.randomRiddles objectAtIndex:appDelegate.randomRiddlesCounter];
NSInteger index = [appDelegate.randomRiddles indexOfObject:myObject];
The second assumes that myObject is at least of type id
So you try to convert a pointer to an int. Therefore the warning is issued.

Related

int or NSInteger to NSUInteger for index

I'm trying to (in Xcode5) use the 'removeObjectAtIndex' for an 'MutableArray' which takes an NSUInteger but the variable I'm using is an integer so I casted with (NSUInteger *) but I get a warning that says cast to 'NSUInteger *' (aka unsigned long *) from smaller integer type. I have not casted the variable 'second' in the code to keep the warning there but it is also an integer
-(void) moveObjectAtIndex:(NSUInteger *)oldIndex toNewIndex:(NSUInteger *)newIndex{
id *member = [self.array objectAtIndex:*oldIndex];
[self.array removeObjectAtIndex:*oldIndex];
if ((NSInteger)newIndex >=(self.array.count)) {
newIndex--; //i casted newIndex because I got a warning about ordered comparison of NSUInteger with NSInteger (I'm not sure if this is best solution)
}
[self.array insertObject:member atIndex: *newIndex];
}
-(void)moveObjectInArray:(NSMutableArray *)array{
[array moveObjectAtIndex:(NSUInteger *) first toNewIndex:second];
}
Your use of pointers is all wonky. id* should just be id and NSUInteger* should just be NSUInteger — you don't want a pointer to a pointer to an object or a pointer to an integer here.
What's problem for you of using just NSUInteger rather than NSUInteger* ?
-(void) moveObjectAtIndex:(NSUInteger)oldIndex toNewIndex:(NSUInteger)newIndex{
id member = [self.array objectAtIndex:oldIndex]; //Here is id, id* is wrong
[self.array removeObjectAtIndex:oldIndex];
newIndex = newIndex >= self.array.count ? : self.array.count - 1; // Here should be self.array.count - 1, not newIndex-1
newIndex = MIN(_cloudListArray.count, newIndex);
[self.array insertObject:member atIndex:newIndex];
}
In the following statement
if ((NSInteger)newIndex >=(self.array.count))
you are typecasting pointer to NSInteger. It should be
if ((NSInteger)*newIndex >=(self.array.count))
Still, you should be careful with typecasting and be wary of their consequences due to any signed/unsigned conversion or data loss.
Also, in this line
[array moveObjectAtIndex:(NSUInteger *) first toNewIndex:second];
type of first should be NSUInteger * or of same size. Please note that long is 64-bit in 64-bit environment and typecasting as smaller pointer type to larger pointer type will yield undefined behaviour. Same applies to second. One solution is that use temporary variable and then copy back the result.

discussing functionality of id's stringValue function?

I got a NSMutableArray object with int values
and I can get a certain value via :
int *v0=[[[arrayObj objectAtIndex:0] intValue];
there is no problem.
But
I got a NSMutableArray object with NSString values
and I cannot get a certain value via :
NSString *v0=[[[arrayObj objectAtIndex:0] stringValue];
//raises error
I want to learn and understand exactly what stringValue for... and why this error occurs ?
NSString *v0=[arrayObj objectAtIndex:0];
works as expected.I asusme its some kind of pointer with null terminated so it can leech value.
Im not sure this line is also unicode/encoded string safe code.
in conclusion:
want to know the purpose of stringValue with some lines o code snippets
I got a NSMutableArray object with int values
That's not possible, Cocoa arrays always contain objects. You probably have an array of NSNumber objects that wrap the integers, like:
NSArray *arrayOfNumbers = #[#1, #2, #3];
NSNumber objects have an intValue method, so this works:
int value = [arrayOfNumbers[0] intValue];
On the other hand when you have an array of strings ...
NSArray *arrayOfStrings = #[#"1", #"2", #"3"];
... you want to access individual elements directly, without converting the string object to something else:
NSString *element = arrayOfStrings[0];
NSString objects do not understand the stringValue method:
[arrayOfStrings[0] stringValue]; // crash: does not recognize selector
Back at the beginning, our NSNumber objects from the first array do understand stringValue. You can use it to convert the number to a string:
NSString *intString = [arrayOfNumbers[0] stringValue];
To make the confusion perfect, NSString also understand the intValue message:
int value = [arrayOfStrings[0] intValue];
Here intValue means to try to convert the string to a plain C int value.
The error you will be getting (but failing to post with your question) will be Unknown selector sent to instance and this is because NSString doesn't have a stringValue method.
The approach you suggest is correct:
NSString *v0 = [arrayObj objectAtIndex:0];
EDIT (prompted by #Answerbot's answer):
The reason you are confused is that [NSString intValue] is used to convert the string value to an integer, as long as the string represents an integer (i.e. #"123"). However you don't need this for string as the object is already a string. It's therefore not provided.

How to append NSString wiht number?

I'm new in Cocoa.
I have NSString - (e.g) MUSIC . I want to add some new NSString in Array,
And want to check something like this
if MUSIC already contained in Array, add Music_1 , after Music_2 and so on.
So I need to be able read that integer from NSString, and append it +1 .
Thanks
Use
NSString *newString = [myString stringByAppendingString:[NSString stringWithFormat:#"_%i", myInteger]];
if myString is "music", newString will be "music_1" or whatever myInteger is.
EDIT: I seem to have gotten the opposite meaning from the other answer provided. Can you maybe clarify what it is you are asking exactly?
Check it out:
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"123", #"qqq", nil];
NSString *myString = #"MUSIC";
NSInteger counter = 0;
if ([array containsObject:myString]){
NSString *newString = [NSString stringWithFormat:#"%#_%d", myString, ++counter];
[array addObject:newString];
}
else
[array addObject:myString];
For checking duplicate element in Array you can use -containsObject: method.
[myArray containsObject:myobject];
If you have very big array keep an NSMutableSet alongside the array.Check the set for the existence of the item before adding to the array. If it's already in the set, don't add it. If not, add it to both.
If you want unique objects and don't care about insertion order, then don't use the array at all, just use the Set. NSMutableSet is a more efficient container.
For reading integer from NSString you can use intValue method.
[myString intValue];
For appending string with number you can use - (NSString *)stringByAppendingString:(NSString *)aString or - (NSString *)stringByAppendingFormat:(NSString *)format ... method.
Here's how you convert a string to an int
NSString *myStringContainingInt = #"5";
int myInt = [myStringContainingInt intValue];
myInt += 1;
// So on...

Is it more intelligent to use CFMutableArrayRef in this case?

I need to keep track of integer values in an array, and sometimes I need to modify a value in the array.
Right now I do this:
// Read an integer from the array
NSNumber* num = [NSNumber numberWithInteger:someInteger];
[numArray addObject:num];
To update a value:
// Update an integer in the array
NSNumber* num = [numArray lastObject]; // get old
NSInteger numInt = [numInt integerValue]; // convert
NSNumber* newNum = [NSNumber numberWithInteger:numInt + someAddition]; // create new
[numArray removeLastObject]; // remove old
[numArray addObject:newNum]; // set new
This is so bad. So many objects involved and all because NSNumber is immutable. Now I thought maybe I must step down into good old C and use CFMutableArrayRef? Can I store simple NSInteger values in there without all this overhead?
You could use replaceObjectAtIndex: withObject: command:
[numArray replaceObjectAtIndex:[array count]-1 withObject:newNum];
It's better than your method

How to Properly set an integer value in NSDictionary?

The following code snippet:
NSLog(#"userInfo: The timer is %d", timerCounter);
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:timerCounter] forKey:#"timerCounter"];
NSUInteger c = (NSUInteger)[dict objectForKey:#"timerCounter"];
NSLog(#"userInfo: Timer started on %d", c);
produces output along the lines of:
2009-10-22 00:36:55.927 TimerHacking[2457:20b] userInfo: The timer is 1
2009-10-22 00:36:55.928 TimerHacking[2457:20b] userInfo: Timer started on 5295968
(FWIW, timerCounter is a NSUInteger.)
I'm sure I'm missing something fairly obvious, just not sure what it is.
You should use intValue from the received object (an NSNumber), and not use a cast:
NSUInteger c = [[dict objectForKey:#"timerCounter"] intValue];
Dictionaries always store objects. NSInteger and NSUInteger are not objects. Your dictionary is storing an NSNumber (remember that [NSNumber numberWithInteger:timerCounter]?), which is an object. So as epatel said, you need to ask the NSNumber for its unsignedIntegerValue if you want an NSUInteger.
Or like this with literals:
NSUInteger c = ((NSNumber *)dict[#"timerCounter"]).unsignedIntegerValue;
You must cast as NSNumber first as object pulled from dictionary will be id_nullable and so won't respond to the value converting methods.