Okay, I am having a hard time with this. I've searched for the past hour on it and I don't get what I am doing wrong. I'm trying to take the currentTitle of a sender, then convert it to an integer so I can use it in a call to list.
NSString *str = [sender currentTitle];
NSInteger *nt = [str integerValue]; // this is where the error appears //
NSString *nextScreen = [NSString stringWithFormat:#"Screen_%#.jpg", [screenList objectAtIndex:nt]];
I assume it's something with the [str integerValue] bit not being properly used, but I can't find an example that works.
Thanks!
Let's analyze the error message:
Initialization (NSInteger nt) makes pointer (*) from integer ([str integerValue]) without a cast.
This means that you are trying to assign a variable of non-pointer type ([str integerValue], which returns an NSInteger) to a variable of pointer type. (NSInteger *).
Get rid of the * after NSInteger and you should be okay:
NSString *str = [sender currentTitle];
NSInteger nt = [str integerValue]; // this is where the error appears //
NSString *nextScreen = [NSString stringWithFormat:#"Screen_%#.jpg", [screenList objectAtIndex:nt]];
NSInteger is a type wrapper for the machine-dependent integral data type, which is defined like so:
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Related
I get a warning when I compile my code, and I'm not sure how to resolve it.
warning: incompatible integer to pointer conversion initializing
'unsigned long *' with an expression of type 'unsigned long
_Nullable'
NSDictionary *dict = #{#"foo": #420};
unsigned long *num = [[dict objectForKey:#"foo"] unsignedLongValue];
NSString *oct = [NSString stringWithFormat:#"%o", num];
NSLog(#"%04u", [oct intValue]); // 0644
The output is correct (I'm converting a number to octal format), but I guess my code isn't up to par with the compiler.
Two mistakes here -
1) unsigned long is not an object but primitive.
You simply need to remove the "*" before num as follow -
unsigned long num = [[dict objectForKey:#"foo"] unsignedLongValue];
2) The format of unsigned long is %lu and not %o as you mentioned.
NSString *oct = [NSString stringWithFormat:#"%lu", num];
So, the correct code should be -
NSDictionary *dict = #{#"foo": #420};
unsigned long num = [[dict objectForKey:#"foo"] unsignedLongValue];
NSString *oct = [NSString stringWithFormat:#"%lu", num];
NSLog(#"%04u", [oct intValue]); // 0644
I believe that this should work (the warnings went away):
unsigned long num = (unsigned long)[[dict objectForKey:#"foo"] unsignedLongValue];
NSString *oct = [NSString stringWithFormat:#"%lo", num];
I wrote this method and when I run on arhitecture on 32bit everything is fine, but when I run project on 64Bit I receive warnings and my application crash with error : "Exc_bad_access (code=1 address=0x0)".
This is my method :
+ (NSArray*) getFieldsForClass:(Class)class
{
static NSCharacterSet* commaset;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
commaset = [NSCharacterSet characterSetWithCharactersInString:#","];
});
NSUInteger *raw_propertyCount;
objc_property_t* raw_properties = class_copyPropertyList(class, &raw_propertyCount);
NSMutableArray* properties = [NSMutableArray new];
for (int i=0; i<raw_propertyCount; i++) {
NSString* propertyName = [NSString stringWithCString:property_getName(raw_properties[i]) encoding:NSUTF8StringEncoding];
[properties addObject:propertyName];
}
free(raw_properties);
return properties;
}
Application crash at this line :
NSString* propertyName = [NSString
stringWithCString:property_getName(raw_properties[i])
encoding:NSUTF8StringEncoding];
Can you help me to fix this crash, please ?
Thanks a lot for your help.
objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)
expects a pointer to an unsigned int as last argument, therefore you have to replace
NSUInteger *raw_propertyCount;
by
unsigned int raw_propertyCount;
On the 32-bit iOS platform, a pointer and an int have the same size, therefore it
worked just by chance.
In C# I can convert any char from my string to integer in the following manner
intS="123123";
int i = 3;
Convert.ToInt32( intS[i].ToString());
What is the shortest equivalent of this code in Objective-C ?
The shortest one line code I've seen is
[NSNumber numberWithChar:[intS characterAtIndex:(i)]]
Many interesting proposals, here.
This is what I believe yields the implementation closest to your original snippet:
NSString *string = #"123123";
NSUInteger i = 3;
NSString *singleCharSubstring = [string substringWithRange:NSMakeRange(i, 1)];
NSInteger result = [singleCharSubstring integerValue];
NSLog(#"Result: %ld", (long)result);
Naturally, there is more than one way to obtain what you are after.
However, As you notice yourself, Objective-C has its shortcomings. One of them is that it does not try to replicate C functionality, for the simple reason that Objective-C already is C. So maybe you'd be better off just doing what you want in plain C:
NSString *string = #"123123";
char *cstring = [string UTF8String];
int i = 3;
int result = cstring[i] - '0';
NSLog(#"Result: %d", result);
It doesn't explicitly have to be a char. Here is one way of doing it :)
NSString *test = #"12345";
NSString *number = [test substringToIndex:1];
int num = [number intValue];
NSLog(#"%d", num);
Just to provide a third option, you can use NSScanner for this too:
NSString *string = #"12345";
NSScanner *scanner = [NSScanner scannerWithString:string];
int result = 0;
if ([scanner scanInt:&result]) {
NSLog(#"String contains %i", result);
} else {
// Unable to scan an integer from the string
}
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];
I have some data into a string and I wish to store that data in an integer array... Below is the code.
int valMines[256];
// 'b' is NSString with 256 values in it.
for(int i=0; i<[b length]; i++){
valMines[i] = [NSString stringWithFormat:#"%d", [b characterAtIndex:i]];
NSLog(#"valMines1 is %#", valMines[i]);
}
I am getting a warning and due to that my application is not getting loaded:
Assignment makes integer from pointer without a cast.
Please help
Your valMins is an integer array and you are assigning NSString to it. Probably you are looking something like this:
unichar valMines[256]; // make it unichar instead of int
// 'b' is NSString with 256 values in it.
for(int i=0; i<[b length]; i++){
valMines[i] = [b characterAtIndex:i]; // get and store the unichar
NSLog(#"valMines1 is %d", valMines[i]); // format specifier is %d, not %#
}