Make a two-digit string from a single-digit integer
I believe that the stringWithFormat specifiers are the standard IEEE printf specifiers. Have you tried [NSString stringWithFormat:@”%02d”, 1];
I believe that the stringWithFormat specifiers are the standard IEEE printf specifiers. Have you tried [NSString stringWithFormat:@”%02d”, 1];
You need to escape the non-ASCII characters in your hardcoded URL as well: //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString* stringURL = [NSString stringWithFormat:@”http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false”, webName]; NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL* url = [NSURL URLWithString:webStringURL]; You can probably remove the escaping of the localisationName since it will be handled by … Read more
Here’s one way that doesn’t rely on the limited precision of attempting to parse the string as a number: NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound) { // newString consists only of the digits 0 through 9 } See +[NSCharacterSet decimalDigitCharacterSet] and -[NSString rangeOfCharacterFromSet:].
Typically NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString; and CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString; Now, if you want to know why the __bridge keyword is there, you can refer to the Apple documentation. There you will find: __bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. __bridge_retained or CFBridgingRetain casts an … Read more
You could use: NSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:@” ” withString:@””];
isEqual: compares a string to an object, and will return NO if the object is not a string. isEqualToString: is faster if you know both objects are strings, as the documentation states: Special Considerations When you know both objects are strings, this method is a faster way to check equality than isEqual:. isEqualTo<Class> is used … Read more
I just ran into the same issue when overloading -description for a subclass of NSObject. In this situation I was able to use carriage return (\r) instead of newline (\n) to create a line break. [NSString stringWithFormat:@”%@\r%@”, mystring1,mystring2];
Use the -isEqualToString: method to compare the value of two strings. Using the C == operator will simply compare the addresses of the objects. if ([category isEqualToString:@”Some String”]) { // Do stuff… }
Get c-string out of std::string for conversion: NSString *errorMessage = [NSString stringWithCString:REALM.c_str() encoding:[NSString defaultCStringEncoding]];
A quick and “dirty” (removes everything between < and >) solution, works with iOS >= 3.2: -(NSString *) stringByStrippingHTML { NSRange r; NSString *s = [[self copy] autorelease]; while ((r = [s rangeOfString:@”<[^>]+>” options:NSRegularExpressionSearch]).location != NSNotFound) s = [s stringByReplacingCharactersInRange:r withString:@””]; return s; } I have this declared as a category os NSString.