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.
Related
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.
I'm learning objective-c.
I tried to grab the entry of this NSMutableDictionary by using numbers as the key:
// [game analysisData] returns the Dictionary
NSMutableDictionary *data = [game analysisData];
NSLog(#"Data:\n%#", data);
// Try using "1" as the key.
id move = [data objectForKey:"1"];
NSLog(#"Move:\n%#", move);
But bad output:
move == (null).
So I can't access it with the string "1". Using an int doesn't work (compilation error).
Do you know how I can access the entries of this NSMutableDictionary?
Here's what the description looks like in FLEXTool:
You can use a NSNumber instead. You can write it like this :
NSNumber * key1 = #1;
NSNumber * key2 = #(1 + 1);
int intKey = 3;
NSNumber * key3 = #(intKey);
You can also do it with a string like this :
id move = [data objectForKey:#"1"]; // you forgot the #
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];
I have an NSNumber like this for example = 1978, i would like to convert this for : 1K9, seconde example : 35700 convert to : 35K7 ( where "k" is kilometers and "M" is meters, how i can do this
thanks
int temp;
NSNumber *yourNumber;//the number you enter from some where
NSString *newValue;
if([yourNumber intValue]>1000){
temp = [yourNumber intValue] % 1000 ;//your number module 1000
newValue= [[temp stringValue]stringByAppendingString:#"K"];
}
Note: I haven't my mac with me, if the [temp stringValue] gives any worning&error please inform me.
Here's how:
NSNumber *initialNumber = [NSNumber numberWithInt:35700];
NSString *resultString = [NSString stringWithFormat:#"%iK%i", floor(initialNumber / 1000), floor((initialNumber % 1000) / 100)];
Basically you can work with the internal number data.
Assuming you are working on a meter-based value, you might want something like this:
NSNumber *sourceValue = ... // your NSNumber value from any source
int meters = sourceValue.intValue;
int km = floor(meters / 1000); // only your kilometers
int sub_km = meters % 1000; // only the part behind the kilometers
int first_sub_km = floor(sum_km / 100); // the first digit of the subrange
NSString *readable = [NSString stringWithFormat:#"%iK%i", km, first_sub_km];
First, you split the meters into <= 1000 and > 1000.
Then you'll just have to put that out formatted, with a K in between.
Write your own subclass of NSNumberFormatter. In this subclass you can implement the calculation logic.
The logic might look like this.
Devide the value by thousend and add your "k"
if you want to have the first digit of hundreds get the thired last digit of your value
return the new string
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.