Objective-C, unsigned long _Nullable - objective-c

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];

Related

Get a char from NSString and convert to int

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
}

Objective-C ASCII decimal integer to NSString conversion

How can I convert and ASCII decimal integer to an NSString in Objective-C? (Example: 36 to "$")
Thanks
Try this:
unichar asciiChar = 36;
NSString *stringWithAsciiChar = [NSString stringWithCharacters:&asciiChar length:1];
You can use the following:
NSString *s = [NSString stringWithFormat:#"%c", 36];
NSLog(#"%#", s); // $

unsigned long long to double

I am trying to convert an unsigned long long to a double, because I need the comma.
NSFileManager* fMgr = [[NSFileManager alloc] init];
NSError* pError = nil;
NSDictionary* pDict = [ fMgr attributesOfFileSystemForPath:NSHomeDirectory() error:&pError ];
//get DiskSpace
NSNumber* pNumAvail = (NSNumber*)[ pDict objectForKey:NSFileSystemSize ];
[fMgr release];
//byte to Mega byte
unsigned long long temp = [pNumAvail unsignedLongLongValue]/1000000;
//Mega byte to kilo byte
double tempD = (double)(temp/1000.0);
NSLog([NSString stringWithFormat:#"%qu", temp]); //result 63529
NSLog([NSString stringWithFormat:#"%i", tempD]); //result 1168231105
///////////////////////////////////////////////////but i want 63.529
What am I doing wrong?
You are mismatching your format specifier. You need to use a floating-point format to print a double. Try using %f instead of %i. The mismatch causes undefined behaviour.
I think your format is wrong. You should use %f : NSLog([NSString stringWithFormat:#"%f", tempD]);

How do you convert an NSUInteger into an NSString?

How do you convert an NSUInteger into an NSString? I've tried but my NSString returned 0 all the time.
NSUInteger NamesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];
NSLog(#"--- %d", NamesCategoriesNSArrayCount);
[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:#"%d", NamesCategoriesNSArrayCount]];
NSLog(#"=== %d", NamesCategoriesNSArrayCountString);
When compiling with support for arm64, this won't generate a warning:
[NSString stringWithFormat:#"%lu", (unsigned long)myNSUInteger];
I hope your NamesCategoriesNSArrayCountString is NSString;
if yes use the below line of code.
NamesCategoriesNSArrayCountString = [NSString stringWithFormat:#"%d", NamesCategoriesNSArrayCount]];
istead of
[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:#"%d", NamesCategoriesNSArrayCount]];
When compiling for arm64, use the following to avoid warnings:
[NSString stringWithFormat:#"%tu", myNSUInteger];
Or, in your case:
NSUInteger namesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];
NSLog(#"--- %tu", namesCategoriesNSArrayCount);
[namesCategoriesNSArrayCountString setText:[NSString stringWithFormat:#"%tu", namesCategoriesNSArrayCount]];
NSLog(#"=== %#", namesCategoriesNSArrayCountString);
(Also, tip: Variables start with lowercase. Info: here)
You can also use:
NSString *rowString = [NSString stringWithFormat: #"%#", #(row)];
where row is a NSUInteger.
This String Format Specifiers article from Apple is specific when you need to format Apple types:
OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.
[NSString stringWithFormat:#"%ld", (long)value] : NSInteger displayed as decimal
[NSString stringWithFormat:#"%lx", (long)value] : NSInteger displayed as hex
[NSString stringWithFormat:#"%lu", (unsigned long)value] : NSUInteger displayed as decimal
[NSString stringWithFormat:#"%lx", (unsigned long)value] : NSUInteger displayed as hex
[NSString stringWithFormat:#"%f", value] : CGFloat
[NSString stringWithFormat:#"%ld", (long)value] : CFIndex displayed as decimal
[NSString stringWithFormat:#"%lx", (long)value] : CFIndex displayed as hex
See the article for more details.

initialization makes pointer from integer without a cast

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