pointer to integer - objective-c

I have an array of integer and i'm trying to get an element from the array; xcode keeps showing this message: "initialization makes integer from pointer without a cast"
I know that this warning means that i can't alloc integer to pointer type, and i'm asking how ca i get that element, or if there is a solution to convert pointer to integer
Here is my code:
NSString *pathvalidrep = [[NSBundle mainBundle] pathForResource:#"validrep" ofType:#"plist"];
NSArray *tabreponses = [[NSArray arrayWithContentsOfFile:pathvalidrep] retain];
truerep = tabreponses;
[tabreponses release];
NSUInteger val1 = [truerep objectAtIndex:0]; //the warning apears here
thanx for help :)

Elements of the array are probably NSNumber objects... And the warning is appearing because of this, you assign a pointer to NSNumber to a NSUInteger.
Try :
NSUInteger val1 = [[truerep objectAtIndex:0] integerValue];

Related

Going crazy with UITextField

I'm literally going crazy whit these six rows of code.
NB: nome and prezzo are 2 textFields
NSString *itemName = (NSString *) [rowVals objectForKey:#"name"];
NSString *itemPrice = (NSString *) [rowVals objectForKey:#"price"];
nome.text = itemName;
nome.userInteractionEnabled = YES;
prezzo.text = itemPrice;
prezzo.userInteractionEnabled = YES;
Don't know why when itemPrice is copied in one of those label, the program go in SIGABRT.
Instead if I try to read the content with an NSLog(#"%#",itemPrice); it return the exact value, so it means that is a valid NSString.
The only solution I found is passing through a NSNumber:
NSNumber *itemPrice = (NSNumber *) [rowVals objectForKey:#"price"];
prezzo.text = [[NSString alloc] initWithFormat:#"%#", itemPrice];
There is another way to use directly the NSString?
Probably the value in the #"price" field is NSNumber, and not an NSString. The NSLog method will still provide a correct result, since %# is used for any NSObject subclass, not just NSString.
How about this:
NSString *itemPrice = [[rowVal objectForKey:#"price"] stringValue];
prezzo.text = itemPrice;
The problem might be the object type returned by [rowVals objectForKey:#"price"]. When you place the (NSString *) cast before the method call, you're telling the compiler what type of object is returned, but not actually converting it into an NSString. The line you use below does convert from NSNumber (or whatever other object) to a string: [[NSString alloc] initWithFormat:#"%#", itemPrice]
You might be storing NSNumber's object in NSDictionary instead of NSString.
There could be 2 ways: one would be to convert NSNumber to NSString while adding it to dictionary or the other way would be to convert NSNumber to NSString while assigning it to "itemName".
you may do the conversion for second option like:
NSString *itemPrice = [[rowVals objectForKey:#"price"]stringValue];

Initialization makes integer without a cast

I get this warning and I am not sure how to fix it. The line where I get the warning is
NSInteger thescore = [[myDictionary objectForKey:#"Score"] objectAtIndex:0];
If it makes a difference, myDictionary is a NSDictionary
Edit: How is this? Btw my array is a NSMutableArray and not a NSArray
NSInteger thescore = [[myDictionary valueForKey:#"Score"] integerValue];
Edit 2: #Bavarious mentioned that I should do the following:
int thescore = [[myDictionary objectForKey:#"Score"] integerValue];
The result of call [someArray objectAtIndex:0] is an object. And NSInteger is a primitive type:
typedef long NSInteger;
(cmd + double-click on NSInteger in xcode to see the definition)
I guess you might actually be storing NSNumber objects in your array. In this case, you could do
NSNumber *thescore = [someArray objectAtIndex:0];

How to assign integer from NSMutableArray

I would like to assign NSInteger by using NSMutableArray is there any way to solve this?
It is not working on simulator and cut off when run the application.
NSInteger Section;
NSMutableArray dataSourceSection;
Section = (NSInteger)[dataSourceSection objectAtIndex:2];
Thank you.
A NSMutableArray only stores objects. NSInteger is not an object, but a primitive data type. There is a class NSNumber, however, that can be used instead to store numeric values inside objects. Here's one example.
NSNumber *five = [NSNumber numberWithInteger:5];
NSMutableArray *numbers = [NSMutableArray array];
[numbers addObject:five];
To get the object back and retrieve the integer value use,
NSNumber *firstNumber = [numbers objectAtIndex:0];
NSInteger valueOfFirstNumber = [firstNumber integerValue];
you can't pull an NSInteger out of an NSMutableArray, basically because you can't put anything rather than objects in. in your case NSNumber would be the way to put numbers in NSMutablearray. if you do so you can easily get hold of your object, which is an NSNumber, and convert it to a NSInteger by:
NSMutableArray *array = [[NSMutableArray alloc] init];
// populate the array with NSNumbers
NSInteger number = [[array objectAtIndex:2] intValue];

How do I access floats from a dictionary of floats?

I have a property list (Data.plist) that contains an array of two dictionaries. Each dictionary is filled with key names (Factor 1, Factor 2, etc) and floats (0.87, 1.15, etc.). I am trying to access the numbers stored in the dictionary. First I load the dictionary using:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Data.plist"];
NSArray *plistData = [[NSArray arrayWithContentsOfFile:finalPath] retain];
NSDictionary *dictionaryOne = [plistData objectAtIndex:0];
Actually accessing the numbers stored is where I'm having a problem:
Float32 conversionFactor = [scyToLCMMen objectForKey:"Factor 50"];
I'm getting the error: "incompatible types in initialization". What am I doing wrong? Is it not a Float32?
Objective-c containers can only hold obj-c types, so what you get is not a float for sure. What you probably have is NSNumber object and you need to "extract" plain float value from it:
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];
NSNumber *conversionFactor = [dictionaryOne valueForKey:#"Factor 50"];
float factor = [conversionFactor floatValue];
That's what I'd do
The value is likely an instance of NSNumber.
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];

How to print a single array element in Objective-C?

How to print a array element at particular index in Objective-C? My code looks like this:
NSString *String=[NSString StringWithContentsOFFile:#"/User/Home/myFile.doc"];
NSString *separator = #"\n";
NSArray *array = [String componetntsSeparatedByString:separator];
NSLog(#"%#",array);
I'm able to print the full contents of an array at once, but I want to assign the element at each index into a string, like...
str1=array[0];
str2=array[1];
str3=array[0];...this continues
How do I do this?
You want the objectAtIndex: method. Example:
NSString *str1 = [array objectAtIndex:0];
NSString *str2 = [array objectAtIndex:1];
NSString *str3 = [array objectAtIndex:2];
From the documentation:
objectAtIndex:
Returns the object located at index.
- (id)objectAtIndex:(NSUInteger)index
Parameters
index
An index within the bounds of the receiver.
Return Value
The object located at index.
Discussion
If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.
if this is only for debugging, you could try using po <> in the gdb.
As of clang version 3.3, you can use the [index] notation, so
NSString *str1 = array[0];
would work now. See here for details.