Adding two integer numbers from array - objective-c

Imagine the following code:
NSMutableArray *arr = [NSMutableArray arrayWithObjects:#200, #100, nil];
NSInteger n1 = (NSInteger)[arr objectAtIndex:0];
NSInteger n2 = (NSInteger)[arr objectAtIndex:1];
NSInteger r = n1 + n2;
NSLog(#"n: %lid", r);
The result I am getting is: -4309586476825365866d, the expected one is: 300.
I also tried to evaluate the individual expressions in debugger to check what I am getting when reading the values from the array, like this: po (NSInteger)[arr objectAtIndex:0]. This showed the correct number, yet when trying to sum them both: po (NSInteger)[arr objectAtIndex:0] + (NSInteger)[arr objectAtIndex:0], an invalid result is generated.
Any ideas why? I am very new to Objective-C, so any source where I could get more info about the issue is appreciated.

You cannot cast NSNumber to NSInteger. You have to call integerValue.
And for more than 10 years there is a more convenient array literal syntax as well as a more convenient index subscription syntax
NSArray *arr = #[#200, #100];
NSInteger n1 = [arr[0] integerValue];
NSInteger n2 = [arr[1] integerValue];
NSInteger r = n1 + n2;
NSLog(#"n: %ld", r);
And for the string format use either i or d but not both.

Related

Convert NSString value to int

That may sound odd.
I got an NSString value NSString * numb = [self.dataDict valueForKey:#"id"]; and i know, that is it some kind of integer (for example, i need that integer to comparison - if val less or equal then something). I need to know what integer is it.
What i've tried:
NSNumber *numba = [self.dataDict valueForKey:#"id"];
NSLog output - numba is 2038735264
And actually that was 428.
is there any way to achieve the point? Thanks!
That is piece of responseObject:
(
{
id = 3;
dog = "\U041a\U0430\U043a\U043e\U0439-\U0442\U043e \U043c\U0443\U0434\U0430\U043a \U043d\U0430\U043a\U0440\U0443\U0442\U0438\U043b";
image = "cute_dog/116.jpg";
score = 586;
},
{
id = 115;
dog = "\U0422\U0430\U043d\U044f \U041a\U043b\U044e\U043a\U0432\U0438\U043d\U0430";
image = "cute_dog/115.jpg";
score = 481;
},
There are a number of methods you can use to convert an NSString to a number. What numeric type would you like?
NSString *string = self.dataDict[#"id"];
int intValue = string.intValue;
NSInteger integerValue = string.integerValue;
long long longLongValue = string.longLongValue;
Trying something like this.
NSNumber *numba = [NSNumber numberWithInt[self.dataDict valueForKey:#"id"]];
//For string
NSString *stringValue = [numba stringValue];
//For integer
NSInteger integer = [numba integerValue];

NSNumber longLongValue returns 0 erroneously

I've got an NSNumber whose value looks like this in the debugger:
(lldb) po avg
377.57142857142857142857142857142857142
When I ask it for its intValue, I get 377. Fine. But if I ask it for its longLongValue I get 0.
What gives?
Update: The full scenario is that I've got an array of NSNumber objects, and I'm getting its average like this:
NSMutableArray *myNums = nil; //assume this is full of numbers
NSNumber *avg = [myNums valueForKeyPath:#"#avg.self"];
int64_t result = avg.longLongValue; //this gets 0
I've stopped here in the debugger and I can p [avg longLongValue] and get 0. p [avg intValue] gets 377.
Update 2: Here is a reliable reproduction:
NSArray *testArr = #[#(392), #(392), #(339), #(394), #(393), #(394), #(339)];
NSNumber *testAvg = [testArr valueForKeyPath:#"#avg.self"];
int64_t testRes = testAvg.longLongValue;
NSLog(#"Result is %lli", testRes);
int res2 = testAvg.intValue;
NSLog(#"Int result is %i", res2);
The result log is:
Result is 0
Int result is 377
Your po [testAvg class] gives NSDecimalNumber which is the problem.
These links would explain why:
Link 1
Link 2
As link 1 suggested you should do [[testAvg stringValue] longLongVale]

Random number generator method

so I have this method that returns a letter (from A-Z) by creating a random number between 1-26 and then indexing a string array to pull out the letter. The only problem is that it does not generate a new letter when the method is called. How can I generate a new letter every time the method is called, like in a while loop. Here is my code:
-(NSString *)alphaGenerator{
NSUInteger randomLetterInteger = arc4random_uniform(26);
NSArray *alphabet = #[#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z"];
NSString *alpha = alphabet[randomLetterInteger];
return alpha;
}
Also, how do I convert a number that is returned from the count method into a number that I can plug into the arc4random_uniform method? I receive the 'incompatible integer to pointer inversion initializing...' error. This is what I have for this:
-(NSUInteger)numOfDictions{
NSString *alpha = [self alphaGenerator];
NSUInteger numb = [cuisines[alpha] count];
NSUInteger *randomNumOfDictions = arc4random_uniform(numb);
return *randomNumOfDictions;
}
These two lines:
NSUInteger *randomNumOfDictions = arc4random_uniform(numb);
return *randomNumOfDictions;
should be:
NSUInteger randomNumOfDictions = arc4random_uniform(numb);
return randomNumOfDictions;
NSInteger is not an object type. It's a primitive type.

Correct format for number (CoreData)?

In CoreData I set type:
Integer 16
and inserted int like this:
[NSNumber numberWithInt:9]
and problem is when I get result from CoreData result is not 9, result is: 114726928
Does andbody know's why?
try the following:
NSInteger number = 9;
NSNumber *numberObject = [NSNumber numberWithInteger:number];
NSInteger numberFromNSNumber = [numberObject integerValue]; // it will contains the 9
it should be fine.

Simple NSInteger and NSMutableArray question

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.