Stanford University iOS open class summary (two): turn the card Demo

This lesson introduces the lazy lazy instantiation (lazy), array, dictionary, the use of class methods, in the end shows a small demo.

Lazy loading (lazy instantiation)

Lazy loading: initialization at the moment before the instance variable is used. Prevents a large number of instance variables (attributes) from being initialized at the same time (especially if you don’t have to write all the methods of creating objects in the viewDidLoad: method).

@interface (ViewController) @property (nonatomic, strong) NSMutableArray *cards; @end @implementation ViewController (NSMutableArray * cards) {/ / if the instance variable is empty, then the initialization; otherwise, directly call if (_cards! = [[NSMutableArray) {_cards alloc] init]}}; return _cards; @end

Array usage

Inserting elements in a variable array

Inserting an element in an array is a method of changing a variable, since an immutable array cannot be changed after initialization. In particular,
notes that when inserting an element in an array, the inserted element must not be empty, otherwise it will cause the program to crash. It is necessary to judge whether the element to be inserted is null!

- (void) addCard: (Card *) card atTop: (BOOL) atTop if (atTop) {{/ / array inserted into the first position of [ insertObject:card atIndex:0]}else{; / / add to the end of the array [ addObject:card];}}

Referencing and removing array elements in a variable array

In an array, you need to be aware of the need to determine whether the array is empty if you reference or remove an element when the array is empty! Therefore, the number of target array elements.

- (Card * drawRandomCard) {/ / if the array is empty array, directly back to the nil, because there is no extraction elements. Card *randomCard = nil; / / determine the number of array elements is 0, if 0, it returns nil if ([ count]) {/ / count] [ to generate 0 random numbers NSUInteger = arc4random (index)% [ count]; / / index subscript reference element randomCard =[index]; / / remove index the elements of the index [ removeObjectAtIndex:index] return randomCard;}};

Class method

The class method is also called the factory method. The class method mainly includes two kinds:

  1. Class initialization: an instance of a class.
  2. Tool method: get some data without instantiation.

Application: use the class method to generate four different colors:

+ (NSArray * ValidSuits) {return @[@ ".", "the" @ @ @ "," highly ""];} - (void) setSuit: (NSString * suit) {if ([[PlayingCard ValidSuits] containsObject:suit]) {_suit = suit;}}


The purpose of instancetype is to ensure that the returned object is the same as the object to which the message was sent.

How do I say this?

It is often used in the return value of a class’s initialization method, because if the class initialization return value is ID, then the pointer can point to any object, so it is possible to point to an object of this type;

So, instancetype as the return value initialization method, then the initial results will be consistent with the type of.


- (instancetype) init {/ / check whether the parent class is initialized successfully self = [super init]; if (self) {return} / / initialization code self;}

Flop Demo

Design requirements:

On the interface to display a card, click flop: if it is positive, click on the back of the show; if it is on the back, click on the display front.

Design sketch´╝Ü

Stanford University iOS open class summary (two): turn the card Demo
left: back of the card; right: front of the card

Implementation code:

1 click button flop

- (IBAction) touchCardButton: (UIButton * sender) {/ / the first judgment card: if the title button is for positive words, if ([sender.currentTitle length]) {//1. back UI: //1.1 set the background for [sender setBackgroundImage:[UIImage imageNamed:@ on the back of the picture "cardBack" forState:UIControlStateNormal]; //1.2 button title is empty. Because of the need to turn to the back of the [sender setTitle:@ "forState:UIControlStateNormal]}else{//2."; UI positive effect: //2.1 set the background to the empty [sender setBackgroundImage:[UIImage imageNamed:@ "forState:UIControlStateNormal]"]; //2.2 set the background color to white [sender setBackgroundColor:[UIColor whiteColor]]; //2.3 title is the set button color of the sign "A [sender setTitle:@ forState:UIControlStateNormal];} / / highly" flop record of the number of. Simultaneous setter and getter methods. First getter method to the current number of times, and then use the setter method to make the number of times +1 self.flipCount++;}

2 Update button UI

Update button UI code into the property flipCount setter method.

- (void) setFlipCount: (int flipCount) {_flipCount = flipCount; / / in the setter method, a label shows the number self.flipsLabel.text [NSString stringWithFormat:@ = "Flips:%d self.flipCount]";}

But I personally think that this is not a good way to deal with, because a method is best to do only one thing, it should be updated button UI code extracted separately as another method:

- (IBAction) touchCardButton: (UIButton * sender) {/ / the first judgment card: if the title button is for positive words, if ([sender.currentTitle length]) {/ / UI [sender setBackgroundImage:[UIImage imageNamed:@ on the back of the "cardBack" forState: UIControlStateNormal] [sender "; setTitle:@ forState:UIControlStateNormal];}else{UI [sender setBackgroundImage:[UIImage / / positive effect imageNamed:@" "forState:UIControlStateNormal] [sender setBackgroundColor:[UIColor; whiteColor]]; sender [setTitle:@" A highly "forState:UIControlStateNormal];} / / flop number +1. Self.flipCount++; / / update Label digital display [self updateFlipsLabel];} / * * * separate extraction method to update the Label display digital * - (void) updateFlipsLabel [NSString stringWithFormat:@ {self.flipsLabel.text = "Flips:%d self.flipCount]";}

As a result, the program is a bit more readable. And we don’t have to rewrite the flipsCount setter method.

This article has been printed on the copyright, for reprint please visit the copyright print. Forty-eight million four hundred and twenty-two thousand nine hundred and twenty-eight

Obtain authorization