Returning an NSString from an NSError
-[NSError localizedDescription]. (Also, every ObjC object inherited from NSObject implements -description which returns an NSString.)
-[NSError localizedDescription]. (Also, every ObjC object inherited from NSObject implements -description which returns an NSString.)
Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project. Example NSString *originalString = [NSString stringWithFormat:@”test”]; NSData *data = … Read more
The NSArray and the contained NSString objects are all immutable. There’s no way to change the objects you have. Instead you have to create new strings and put them in a new array: NSMutableArray *trimmedStrings = [NSMutableArray array]; for (NSString *string in arrayRefineSubjectCode) { NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; [trimmedStrings addObject:trimmedString]; } arrayRefineSubjectCode = … Read more
NSString *command = @”72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553″; command = [command stringByReplacingOccurrencesOfString:@” ” withString:@””]; NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole_byte; char byte_chars[3] = {‘\0′,’\0′,’\0’}; int i; for (i=0; i < [command length]/2; i++) { byte_chars[0] = [command characterAtIndex:i*2]; byte_chars[1] = [command characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL, 16); [commandToSend appendBytes:&whole_byte length:1]; } NSLog(@”%@”, commandToSend);
Yes there is. Adjacent strings will be concatenated for you by the compiler. NSString *info = [NSString stringWithFormat:@”\n Elapsed Time \n” “Battery Level: \n” “Torque: \n” “Energy Used \n” “Energy Regenerated:\n Cadence: \n” “Battery Temp: \n” “Motor Temp: \n” “Incline: \n Speed MPH: \n” “Speed KPH:\n” “Avg Speed MPH: %f \n” “Avg Speed KPH:\n” “Distance … Read more
Sure, you just need to escape the quotation mark. NSString *someString = @”This is a quotation mark: \””; NSLog(@”%@”, someString ); Output: This is a quotation mark: “
This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace]; to get a copy minus leading whitespace. (Code is untested, may require some minor debugging.) @interface NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace; @end @implementation NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace { NSInteger i = 0; while ((i < [self length]) && … Read more
I know this is a very late reply, but you can get the substring “http://www.abc.com” with the following code: [@”abc xyz http://www.abc.com aaa bbb ccc” substringWithRange:NSMakeRange(8, 18)] Of course, you can still use an NSString for that.
It’s good practice to turn literals into constants because: It helps avoid typos, like you said If you want to change the constant, you only have to change it in one place I prefer using static const NSString* static NSString* const because it’s slightly safer than #define. I tend to avoid the preprocessor unless I … Read more
Assuming the following inputs: NSString *myString = @”My blue car is bigger then my blue shoes or my blue bicycle”; NSString *original = @”blue”; NSString *replacement = @”green”; The algorithm is quite simple: NSRange rOriginal = [myString rangeOfString:original]; if (NSNotFound != rOriginal.location) { myString = [myString stringByReplacingCharactersInRange:rOriginal withString:replacement]; }