@property (strong,nonatomic) Nsmutablearray *cards;
@end
@implementation Deck
-(Nsmutablearray *) cards
{
if (!_cards) {_cards=[[nsmutablearray alloc]init];
}
return _cards;
}
Lazy initialization of lazy initialization can improve memory usage efficiency
-(void) Addcard: (Card *) Card Attop: (BOOL) Attop
{
if (attop) {
[Self.cards Insertobject:card atindex:0];
}else
{
[Self.cards Addobject:card];
}
}
[Self.cards insertobject:card atindex:0] insertobject ... atindex. is to insert an object in a mutable array, placed under the subscript "Atindex:". The recipient of the local message is Nsmutablearray, the passed parameter is an element (ID) and Nsuintege, and no object is returned;
[Self.cards Addobject:card]; AddObject: Adds an object after a mutable array (position at the end of the array) message recipient Nsmutablearray, passed in parameter: element (ID), no object returned
-(Card *) Drawrandomcard
{
Card * Rondomcard =nil;
if ([Self.cards count]) {
unsigned index=arc4random ()%[self.cards count];
Rondomcard=self.cards[index];
[Self.cards Removeobjectatindex:index];
}
return rondomcard;
}
[Self.cards Count] The number of elements in the array element message accepts the object is the Nsarray and the subclass object returns the object as #, no incoming parameters
A=arc4random ()%x The X range class randomly an integer if x cannot be a 0 function
[Self.cards Removeobjectatindex:index]; Removeobjectatindex:number rejects the element labeled number in the variable array. The incoming parameter is Usuinteger the recipient of the message is Nsmutablearray, and no object is returned
-(NSString *) conetens
{
Nsarray *rankstrings= [Playingcard rankstring];
return [Rankstrings[self.rank] stringByAppendingString:self.suit];
}
[Rankstrings[self.rank] stringByAppendingString:self.suit]; Stringbyappendingstring is a two string that is merged into a new string returned as NSString accepts an object for nsstring incoming parameter for nsstring;
-(void) Setsuit: (NSString *) suit
{
if ([[[Playingcard Validsuits] containsobject:suit]) {
_suit=suit;
}
}
[[[Playingcard validsuits] containsobject:suit] Containsobject Check if the object is contained in an array the message recipient is the Nsarray incoming parameter is returned as BOOL for the ID
-(NSString *) suit
{
Return _suit? _suit:@ "?";
}
_suit? _suit:@ "?" is equal to
if (_suit) {
_suit}else
{@"?"}
@implementation Playingcarddeck
-(Instancetype) init
{
Self =[super init];
if (self) {
For (NSString *suit in [Playingcard validsuits]) {
for (Nsuinteger rank=1; Rank<=[playingcard Maxrank]; rank++) {
Playingcard *card =[[playingcard Alloc]init];
Card.rank =rank;
Card.suit =suit;
[Self addcard:card];
}
}
}
return self;
}
@end
Instancetype represents the same type of return and the recipient of the message;
The Super keyword is a method that directly implements the superclass;
-(Instancetype) init
{
Self =[super init];
if (self) {...}
return self;
} is the standard overloaded Init method template, which first invokes the initialization method of the parent class. Executes the initialization method of the parent class so that the inherited instance variable is initialized properly, and the execution result of the parent class Init method must be assigned to self, because the initialization process alters the position of the object in memory (meaning that the reference will be changed).
The initialization succeeds, the return value will be non-null, through the IF statement can be verified, can .... code block location into the custom initialization code. You can typically create and initialize instance variables in this location. PS is still not very clear hope behind the hit code will understand.
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
The life cycle of the view. Explain later
#import "ViewController.h"
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet UILabel *flipslabel;
@property (nonatomic) int flipcount;
@end
-(void) Setflipcount: (int) Flipcount
{
_flipcount =flipcount;
Self.flipslabel.text=[nsstring stringwithformat:@ "flip:%d", self flipcount];
NSLog (@ "%d", self.flipcount);
}
Iboutlet means that the uilabel of the view is connected to the controller, and the *flipslabel properties change the Uilabel of the view accordingly.
[NSString stringwithformat:@ "flip:%d", self flipcount]; Create a new string @ "flip:%d", self. Flipcount "self. Flipcount will appear in the string content instead of%d. The message accepts the object NSString (Note that this method is a class method, the object recipient is a class non-instance), and the object element (ID) is returned as a new NSString instance;
#import "ViewController.h"
@interface Viewcontroller ()
@property (weak, nonatomic) ibout Let UILabel *flipslabel;
@property (nonatomic) int flipcount;
@end
-(Ibaction) Touchcardbutton: (UIButton *) Sender {
//uiimage *cardimage =[uiimage imagenamed:@ " Back "];
if ([Sender.currenttitle length]) {[Sender setbackgroundimage:[uiimage imagenamed:@ "Back"] forstate: UIControlStateNormal];
[sender settitle:@ "" forstate:uicontrolstatenormal];
}else
{
[sender setbackgroundimage:[uiimage ima genamed:@ "front"] forstate:uicontrolstatenormal];
[sender settitle:@ "a♠" forstate:uicontrolstatenormal];
}
self.flipcount++;
}
Ibaction is a series of methods that are caused by actions in the user interaction view (such as the user pressing button, which pops up another dialog box, which needs to refer to ibaction)
[Sender.currenttitle length] Sender.currenttitle is the element property of UIButton, getter UIButton the contents of title, get a string.
The length method is the number of string letters, the message recipient is a string, there is no argument, return is Nsuinteger
[UIImage imagenamed:@ "Back"] Create a picture picture fame "Go", (first to import the picture folder to change the name of the image to use in the code again)
[Sender setbackgroundimage:[uiimage imagenamed:@ "Back"] forstate:uicontrolstatenormal set UIButton background image,
and set the state (the rule forstate is generally uicontrolstatenormal) the message recipient is UIButton the incoming parameter is uimage and there is no return value
Fragmentation Knowledge Collation