Objective C New stuffs that I dont know

http://www.raywenderlich.com/21320/objectively-speaking-a-crash-course-in-objective-c-ios6           IOS 6

 

1.  To create an array, there’s a handy new shortcut in Objective-C – @[ item1, item 2, item 3 ].

 

@property (nonatomic, strong) NSArray *myQuotes;

// 1 - Add array of personal quotes
 self.myQuotes = @[
                     @"Live and let live",
                     @"Don't cry over spilt milk",
                     @"Always look on the bright side of life",
                     @"Nobody's perfect",
                     @"Can't see the woods for the trees",
                     @"Better to have loved and lost then not loved at all",
                     @"The early bird catches the worm",
                     @"As slow as a wet week"
                     ];
/*
Note: In the old days, you also had to @synthesize your properties after creating them, and in the really old days you had to manually declare your instance variables as well. This is no longer the case – now all you need to do to add a property is add this single line!
*/

 

2. To display a random quote to the screen, you’ll need two things – a reference to the text view so you can set the text appropriately, and notification when the button is tapped.

Add the following to ViewController.h

@property (nonatomic, strong) IBOutlet UITextView *quoteText;

IBOutlet means that quote_text is an object that can be linked to an interface element on the XIB file so that the view controller can access (or change) properties of the interface element

 

 

- (IBAction)quoteButtonTapped:(id)sender;

This particular method returns an IBAction, which is actually defined to void – i.e. the method returns nothing. But IBAction has another special property – it marks the method as something to you can connect to an action on a UI element. In this case, you’ll be hooking things up so when the button gets tapped, this method gets called

 

id is a special type that means “any object that derives from NSObject”. 

ViewController.m

-(IBAction)quoteButtonTapped:(id)sender {
    // 1 - Get number of rows in array
    int array_tot = [self.myQuotes count];
    // 2 - Get random index
    int index = (arc4random() % array_tot);
    // 3 - Get the quote string for the index
    NSString *my_quote = self.myQuotes[index];
    // 4 - Display the quote in the text view
    self.quoteText.text = [NSString stringWithFormat:@"Quote:\n\n%@",  my_quote];
}

arc4random() is a regular C-style function (not a method), so you use the regular parenthesis syntax you know and love. In this case, since you want to randomly select one of the quotes, the highest possible value is the number of rows in the array, and the lowest possible value is 0.

 

3. property list

 

// 2 - Load movie quotes
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"quotes" ofType:@"plist"];
self.movieQuotes= [NSMutableArray arrayWithContentsOfFile:plistCatPath];

valueForKey

-(IBAction)quoteButtonTapped:(id)sender {
    // 1 - Get number of rows in array
    int array_tot = [self.movieQuotes count];
    // 2 - Get random index
    int index = (arc4random() % array_tot);
    // 3 - Get the quote string for the index
    //NSString *my_quote = [self.myQuotes objectAtIndex:index];
    NSString *my_quote = self.movieQuotes[index][@"quote"];
    // 4 - Display the quote in the text view
    self.quoteText.text = [NSString stringWithFormat:@"Quote:\n\n%@",  my_quote];
}

 

4. segmented control

 

@property (nonatomic, strong) IBOutlet UISegmentedControl *quoteOpt;

 

-(IBAction)quoteButtonTapped:(id)sender {
    // 1 - Get personal quotes when the final segment is selected
    if (self.quoteOpt.selectedSegmentIndex == 2) {
        // 1 - Get number of rows in array
        int array_tot = [self.myQuotes count];
        // 2 - Get random index
        int index = (arc4random() % array_tot);
        // 3 - Get the quote string for the index
        NSString *my_quote = self.myQuotes[index];
        // 4 - Display the quote in the text view
        self.quoteText.text = [NSString stringWithFormat:@"Quote:\n\n%@",  my_quote];
    }
}

 

// 2 - Get movie quotes
else {
    // 2.1 - determine category
    NSString *selectedCategory = @"classic";
    if (self.quoteOpt.selectedSegmentIndex == 1) {
        selectedCategory = @"modern";
    }
    // 2.2 - filter array by category using predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", selectedCategory];
    NSArray *filteredArray = [self.movieQuotes filteredArrayUsingPredicate:predicate];
    // 2.3 - get total number in filtered array
    int array_tot = [filteredArray count];
    // 2.4 - as a safeguard only get quote when the array has rows in it
    if (array_tot > 0) {
        // 2.5 - get random index
        int index = (arc4random() % array_tot);
        // 2.6 - get the quote string for the index
        NSString *quote = filteredArray[index][@"quote"];//?
        self.quoteText.text = [NSString stringWithFormat:@"Movie Quote:\n\n%@",  quote];
    } else {
        self.quoteText.text = [NSString stringWithFormat:@"No quotes to display."];
    }
}

 

loop

if (self.quoteOpt.selectedSegmentIndex == 2) {
    // 1.1 - Get array count
    int array_tot = [self.myQuotes count];
    // 1.2 - Initialize string for concatenated quotes
    NSString *all_my_quotes = @"";
    NSString *my_quote = nil;
    // 1.3 - Iterate through array
    for (int x=0; x < array_tot; x++) {
        my_quote = self.myQuotes[x];
        all_my_quotes = [NSString stringWithFormat:@"%@\n%@\n",  all_my_quotes,my_quote];
    }
    self.quoteText.text = [NSString stringWithFormat:@"%@", all_my_quotes];
}

 

if ([quote1 isEqualToString:quote2]) {

 

 

 

 

 

 

 

 

 

 

posted on 2013-01-21 21:42  Chansonyan  阅读(184)  评论(0)    收藏  举报

导航