Search Results

Search found 3 results on 1 pages for 'zigrivers'.

Page 1/1 | 1 

  • Learning Objective-C: Need advice on populating NSMutableDictionary

    - by Zigrivers
    I am teaching myself Objective-C utilizing a number of resources, one of which is the Stanford iPhone Dev class available via iTunes U (past 2010 class). One of the home work assignments asked that I populate a mutable dictionary with a predefined list of keys and values (URLs). I was able to put the code together, but as I look at it, I keep thinking there is probably a much better way for me to approach what I'm trying to do: Populate a NSMutableDictionary with the predefined keys and values Enumerate through the keys of the dictionary and check each key to see if it starts with "Stanford" If it meets the criteria, log both the key and the value I would really appreciate any feedback on how I might improve on what I've put together. I'm the very definition of a beginner, but I'm really enjoying the challenge of learning Objective-C. void bookmarkDictionary () { NSMutableDictionary* bookmarks = [NSMutableDictionary dictionary]; NSString* one = @"Stanford University", *two = @"Apple", *three = @"CS193P", *four = @"Stanford on iTunes U", *five = @"Stanford Mall"; NSString* urlOne = @"http://www.stanford.edu", *urlTwo = @"http://www.apple.com", *urlThree = @"http://cs193p.stanford.edu", *urlFour = @"http://itunes.stanford.edu", *urlFive = @"http://stanfordshop.com"; NSURL* oneURL = [NSURL URLWithString:urlOne]; NSURL* twoURL = [NSURL URLWithString:urlTwo]; NSURL* threeURL = [NSURL URLWithString:urlThree]; NSURL* fourURL = [NSURL URLWithString:urlFour]; NSURL* fiveURL = [NSURL URLWithString:urlFive]; [bookmarks setObject:oneURL forKey:one]; [bookmarks setObject:twoURL forKey:two]; [bookmarks setObject:threeURL forKey:three]; [bookmarks setObject:fourURL forKey:four]; [bookmarks setObject:fiveURL forKey:five]; NSString* akey; NSString* testString = @"Stanford"; for (akey in bookmarks) { if ([akey hasPrefix:testString]) { NSLog(@"Key: %@ URL: %@", akey, [bookmarks objectForKey:akey]); } } } Thanks for your help!

    Read the article

  • initWithCoder works, but init seems to be overwriting my objects properties?

    - by Zigrivers
    Hi guys, I've been trying to teach myself how to use the Archiving/Unarchiving methods of NSCoder, but I'm stumped. I have a Singleton class that I have defined with 8 NSInteger properties. I am trying to save this object to disk and then load from disk as needed. I've got the save part down and I have the load part down as well (according to NSLogs), but after my "initWithCoder:" method loads the object's properties appropriately, the "init" method runs and resets my object's properties back to zero. I'm probably missing something basic here, but would appreciate any help! My class methods for the Singleton class: + (Actor *)shareActorState { static Actor *actorState; @synchronized(self) { if (!actorState) { actorState = [[Actor alloc] init]; } } return actorState; } -(id)init { if (self = [super init]) { NSLog(@"New Init for Actor started...\nStrength: %d", self.strength); } return self; } -(id)initWithCoder:(NSCoder *)coder { if (self = [super init]) { strength = [coder decodeIntegerForKey:@"strength"]; dexterity = [coder decodeIntegerForKey:@"dexterity"]; stamina = [coder decodeIntegerForKey:@"stamina"]; will = [coder decodeIntegerForKey:@"will"]; intelligence = [coder decodeIntegerForKey:@"intelligence"]; agility = [coder decodeIntegerForKey:@"agility"]; aura = [coder decodeIntegerForKey:@"aura"]; eyesight = [coder decodeIntegerForKey:@"eyesight"]; NSLog(@"InitWithCoder executed....\nStrength: %d\nDexterity: %d", self.strength, self.dexterity); [self retain]; } return self; } -(void) encodeWithCoder:(NSCoder *)encoder { [encoder encodeInteger:strength forKey:@"strength"]; [encoder encodeInteger:dexterity forKey:@"dexterity"]; [encoder encodeInteger:stamina forKey:@"stamina"]; [encoder encodeInteger:will forKey:@"will"]; [encoder encodeInteger:intelligence forKey:@"intelligence"]; [encoder encodeInteger:agility forKey:@"agility"]; [encoder encodeInteger:aura forKey:@"aura"]; [encoder encodeInteger:eyesight forKey:@"eyesight"]; NSLog(@"encodeWithCoder executed...."); } -(void)dealloc { //My dealloc stuff goes here [super dealloc]; } I'm a noob when it comes to this stuff and have been trying to teach myself for the last month, so forgive anything obvious. Thanks for the help!

    Read the article

  • Working through exercises in "Cocoa Programming for Mac OS X" - I'm stumped

    - by Zigrivers
    I've been working through the exercises in a book recommended here on stackoverflow, however I've run into a problem and after three days of banging my head on the wall, I think I need some help. I'm working through the "Speakline" exercise where we add a TableView to the interface and the table will display the "voices" that you can choose for the text to speech aspect of the program. I am having two problems that I can't seem to get to the bottom of: I get the following error: * Illegal NSTableView data source (). Must implement numberOfRowsInTableView: and tableView:objectValueForTableColumn:row: The tableView that is supposed to display the voices comes up blank I have a feeling that both of these problems are related. I'm including my interface code here: #import <Cocoa/Cocoa.h> @interface AppController : NSObject <NSSpeechSynthesizerDelegate, NSTableViewDelegate> { IBOutlet NSTextField *textField; NSSpeechSynthesizer *speechSynth; IBOutlet NSButton *stopButton; IBOutlet NSButton *startButton; IBOutlet NSTableView *tableView; NSArray *voiceList; } - (IBAction)sayIt:(id)sender; - (IBAction)stopIt:(id)sender; @end And my implementation code here: #import "AppController.h" @implementation AppController - (id)init { [super init]; //Log to help me understand what is happening NSLog(@"init"); speechSynth = [[NSSpeechSynthesizer alloc] initWithVoice:nil]; [speechSynth setDelegate:self]; voiceList = [[NSSpeechSynthesizer availableVoices] retain]; return self; } - (IBAction)sayIt:(id)sender { NSString *string = [[textField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; //Is the string zero-length? if([string length] == 0) { NSLog(@"String from %@ is a string with a length of %d.", textField, [string length]); [speechSynth startSpeakingString:@"Please enter a phrase first."]; } [speechSynth startSpeakingString:string]; NSLog(@"Started to say: %@", string); [stopButton setEnabled:YES]; [startButton setEnabled:NO]; } - (IBAction)stopIt:(id)sender { NSLog(@"Stopping..."); [speechSynth stopSpeaking]; } - (void) speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)complete { NSLog(@"Complete = %d", complete); [stopButton setEnabled:NO]; [startButton setEnabled:YES]; } - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView { return [voiceList count]; } - (id)tableView: (NSTableView *)tv objecValueForTableColumn: (NSTableColumn *)tableColumn row:(NSInteger)row { NSString *v = [voiceList objectAtIndex:row]; NSLog(@"v = %@",v); NSDictionary *dict = [NSSpeechSynthesizer attributesForVoice:v]; return [dict objectForKey:NSVoiceName]; } /* - (BOOL)respondsToSelector:(SEL)aSelector { NSString *methodName = NSStringFromSelector(aSelector); NSLog(@"respondsToSelector: %@", methodName); return [super respondsToSelector:aSelector]; } */ @end Hopefully, you guys can see something obvious that I've missed. Thank you!

    Read the article

1