How to add percent sign to NSString
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
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
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.
You can do exactly the same call with Swift: Swift 4 & Swift 5 In Swift 4 String is a collection of Character values, it wasn’t like this in Swift 2 and 3, so you can use this more concise code1: let string = “hello Swift” if string.contains(“Swift”) { print(“exists”) } Swift 3.0+ var string … Read more
You can check if [string length] == 0. This will check if it’s a valid but empty string (@””) as well as if it’s nil, since calling length on nil will also return 0.
NSString* str = @”teststring”; NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
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
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