Tag: cocoa

Getting array elements with valueForKeyPath

Is there any way to access an NSArray element with valueForKeyPath? Google’s reverse geocoder service, for example, returns a very complex data structure. If I want to get the city, right now I have to break it into two calls, like this: NSDictionary *address = [NSString stringWithString:[[[dictionary objectForKey:@”Placemark”] objectAtIndex:0] objectForKey:@”address”]]; NSLog(@”%@”, [address valueForKeyPath:@”AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName”]); Just wondering […]

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

Under NSTimeZone class, there is both +localTimeZone and +systemTimeZone. I did a test on iphone simulator, both return NSTimeZone object indicating the same timezone. What is the difference? Which one I should use to find out the timezone setting of the iPhone? Thanks My test: NSLog(@”Local Time Zone %@”,[[NSTimeZone localTimeZone] name]); NSLog(@”System Time Zone %@”,[[NSTimeZone […]

How to incorporate WebSockets into a Cocoa application

I am developing a Cocoa application which involves a chat element. One approach I’ve considered is using Websockets to handle client-server communication. This would be particularly desirable because chats will also be displayed on a website, and using Websockets could make the implementation very simple. So: would it be possible to use a WebView element, […]

Split NSString multiple times on the same separator

I am currently receiving a string like this: @”Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54″ And I am splitting it like this: testArray = [[NSArray alloc] init]; NSString *testString = [[NSString alloc] initWithFormat:@”Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30″]; testArray = [testString componentsSeparatedByString:@”,”]; dict = [NSMutableDictionary dictionary]; for (NSString *s in testArray) { testArray2 = [s componentsSeparatedByString:@”|”]; [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]]; } I will now be […]

Saving custom attributes in NSAttributedString

I need to add a custom attribute to the selected text in an NSTextView. So I can do that by getting the attributed string for the selection, adding a custom attribute to it, and then replacing the selection with my new attributed string. So now I get the text view’s attributed string as NSData and […]

Why tack a protocol of NSObject to a protocol implementation

I have been seeing some code around that resembles the following: @protocol MyProtocol <NSObject> // write some methods. @end Is there any particular reason why MyProtocol conforms to the NSObject protocol? Isn’t that rather redundant in that if you do something such as: id <MyProtocol> foo; // foo here conforms to NSObject AND MyProtocol? Just […]

Getting user's default email address in Cocoa

How do I obtain the user’s default email address? I need to obtain it for my crash reporter dialog, so the user won’t have to fill it out manually.

Is there anything like a generic list in Cocoa / Objective-C?

What I really like in C# are generic lists. A list that can contain only one type of objects. Is there something like a generic list in Cocoa/Objective-C? As far I only know NSArray who will take a pointer to any object.

Cocoa/ Objective-C Shell Command Line Execution

This is probably a stupid question, but how can I execute a shell command from my Cocoa app? I have the command as a string “command”, but can easily manipulate data as needed. There is no need to get a returned output value.

Instance Variables for Objective C Categories

I have a situation where it seems like I need to add instance variables to a category, but I know from Apple’s docs that I can’t do that. So I’m wondering what the best alternative or workaround is. What I want to do is add a category that adds functionality to UIViewControllers. I would find […]