Anyway to get string from variable name?
As easy as #define VariableName(arg) (@””#arg) Then you do: NSObject *obj; NSString *str = VariableName(obj); NSLog(@”STR %@”, str);//obj
As easy as #define VariableName(arg) (@””#arg) Then you do: NSObject *obj; NSString *str = VariableName(obj); NSLog(@”STR %@”, str);//obj
You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate. NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [NSString stringWithString:buffer]; Of course, you can also just make a copy: NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [[buffer copy] autorelease]; … Read more
You can “trim” the text, that is remove all the whitespace at the start and end. If all that’s left is an empty string, then only whitespace (or nothing) was entered. NSString *rawString = [textField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace]; if ([trimmed length] == 0) { // Text was … Read more
You can “trim” the text, that is remove all the whitespace at the start and end. If all that’s left is an empty string, then only whitespace (or nothing) was entered. NSString *rawString = [textField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace]; if ([trimmed length] == 0) { // Text was … Read more
Here’s a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: – (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width; }
First, you might consider using a class to hold your inner array’s strings, or loading it from a plist file (in which it is easy to make an 2d array of strings). For direct declarations, you have a couple of options. If you want to use an NSArray, you’ll have to manually create the structure … Read more
Here’s an example that uses a transform to rotate the drawing context. Essentially it’s just like setting a color or shadow, just make sure to use -concat instead of -set. CGFloat rotateDeg = 4.0f; NSAffineTransform *rotate = [[NSAffineTransform alloc] init]; [rotate rotateByDegrees:rotateDeg]; [rotate concat]; // Lock focus if needed and draw strings, images here. [rotate … Read more
For future reference, this is what I found to work (i.e. encode everything properly) + (NSString*)encodeURL:(NSString *)string { NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(“:/?#[]@!$ &'()*+,;=\”<>%{}|\\^~`”), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); if (newString) { return newString; } return @””; }
I’m pretty sure that the problem here is that the hebrew date in strDate is carrying unicode characters that make it display right-to-left. That’s causing chaos when combined with the ‘ordinary’ left-to-right string in timeForResponse. The date formatter is picking that up from the hebrew locale. Try this: Change your date format string to [dateFormatter … Read more
+URLWithString: produces an NSURL that represents the string as given. So the string might be @”http://www.google.com” and the URL represents http://www.google.com. +fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz. You can of course … Read more