Convert UTF-8 encoded NSData to NSString

If the data is not null-terminated, you should use -initWithData:encoding: NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]; If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end. NSString* newStr = [NSString stringWithUTF8String:[theData bytes]]; (Note that if the input is not properly UTF-8-encoded, you will get nil.) Swift … Read more

How to convert an NSString into an NSNumber

Use an NSNumberFormatter: NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; f.numberStyle = NSNumberFormatterDecimalStyle; NSNumber *myNumber = [f numberFromString:@”42″]; If the string is not a valid number, then myNumber will be nil. If it is a valid number, then you now have all of the NSNumber goodness to figure out what kind of number it actually is.

Constants in Objective-C

You should create a header file like: // Constants.h FOUNDATION_EXPORT NSString *const MyFirstConstant; FOUNDATION_EXPORT NSString *const MySecondConstant; //etc. (You can use extern instead of FOUNDATION_EXPORT if your code will not be used in mixed C/C++ environments or on other platforms.) You can include this file in each file that uses the constants or in the … Read more

Shortcuts in Objective-C to concatenate NSStrings

An option: [NSString stringWithFormat:@”%@/%@/%@”, one, two, three]; Another option: I’m guessing you’re not happy with multiple appends (a+b+c+d), in which case you could do: NSLog(@”%@”, [Util append:one, @” “, two, nil]); // “one two” NSLog(@”%@”, [Util append:three, @”https://stackoverflow.com/”, two, @”https://stackoverflow.com/”, one, nil]); // three/two/one using something like + (NSString *) append:(id) first, … { NSString … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)