How do I convert NSInteger to NSString datatype?
NSIntegers are not objects, you cast them to long
, in order to match the current 64-bit architectures' definition:
NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];
Convert a NSInteger to NSString Xcode iOS8
intValue
is not methods that exist on a NSInteger
but on NSNumber
.
Since NSInteger
is a typedef for a primitive type int
on 32Bit and long
on 64bit it does not have an methods.
Here is how you'd could do it, casting to make sure it works on both 32 and 64 bit devices.
NSString *inStr = [NSString stringWithFormat:@"%ld", (long)clasesDadas ];
Also as stated by meronix the count method will result in an NSUInteger
so you might want to check the type and format accordingly
how to convert NSinteger to String]
Try this:
NSMutableString *myWord = [[NSMutableString alloc] init];
for (int i=0; i<=200; i=i+10) {
[myWord appendString:[NSString stringWithFormat:@"%d", i]];
//...
}
//do something with myWord...
[myWord release];
NSInteger
is simply a typedef
to the int
or long
data types on 32/64-bit systems.
Converting NSInteger and NSString into array of bytes
Since your string is a UUID string you can do something like this:
NSString *test = @"";
uuid_t uuid;
uuid_parse([test UTF8String], uuid)
NSData *data = [NSData dataWithBytes:uuid length:16];
For the number you can do:
NSInteger test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];
Keep in mind that NSInteger
is probably more than two bytes and you may also need to worry about byte order.
Update: Since it seems you need the integer value to be two bytes, you should do:
uint16_t test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];
This will ensure 2 bytes. You also need to worry about byte ordering so you really need:
uint16_t test = 1;
uint16_t bytes = CFSwapInt16HostToBig(test);
NSData *data = [NSData dataWithBytes:&bytes length:sizeof(bytes)];
Change CFSwapInt16HostToBig
to CFSwapInt16HostToLitte
if appropriate.
Convert any data type to NSString
valueForKey:
will never returnint
orfloat
. Instead, it will wrap them intoNSNumber
s. Details here.Therefore, you can use
"%@"
to represent them instringWithFormat:
.You can also use
description
method ofNSObject
like this:[[contact valueForKey:formCell.variableName] description]
.
How can I cast an NSString as a hex representation of NSInteger
Turns out this is deliciously simple:
unsigned int bitshiftString;
[[NSScanner scannerWithString:flags] scanHexInt:&bitshiftString];
Transform NSInteger to NSString as a 5-digit number
Use the %05d
format specifier in order to pad with zeroes:
NSString *myStr = [NSString stringWithFormat:@"%05d", myInt];
Wikipedia's explanation.
Related Topics
Xcode/Simulator: How to Run Older iOS Version
iOS App, Programmatically Get Build Version
How to Create .Ipa File for Testing Using Runner.App
Present Uialertcontroller on Top of Everything Regardless of the View Hierarchy
Icon Already Includes Gloss Effects
Problems with Ssl Pinning and Afnetworking 2.5.0 (Nsurlerrordomain Error -1012.)
Custom Url Scheme for New Facebook iOS App
Using Arc, Is It Fatal Not to Have an Autorelease Pool for Every Thread
How to Delete an App from Itunesconnect/App Store Connect
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
Apply Gradient Color to Arc Created with Uibezierpath
How to Open Location Services Screen from Setting Screen
Nspredicate to Test for Null, and Blank Strings
Xcode Info.Plist Build Variable ${Product_Name:Rfc1034Identifier} Seems Completely Undocumented
Icloud + Coredata - How to Avoid Pre-Filled Data Duplication