Point notation versus method notation

I immerse myself in iOS programming, and I am having difficulty thinking about notation and method notation.

As far as I understand, Dot Notation can be used to call setters / getters by properties and much cleaner to write / read. The Notation method is used to send messages to objects for their management, etc.

Could someone give me a simple explanation of why the following two statements are significantly different from each other, and one will compile, but the other will not work due to a syntax error.

- (IBAction)digitPressed:(UIButton *)sender 
{
   NSString *digit = [sender currentTitle];

   self.display.text = [self.display.text stringByAppendingFormat:digit];
   self.display.text = self.display.text.stringByAppendingFormat:digit;

}

Thank.

+5
source share
6 answers

Objective-C , . Dot - , , , .

. , ( ), .

self.display.text = self.display.text.stringByAppendingFormat:digit;

, stringByAppendingString, stringByAppendingFormat

, , .

:  self.foo.attributeOfMyClass

:  self.foo.downloadSomethingFromAWebsite

, , ( ) , .

+11

- , , . :

  • : foo.bar = 3; [foo setBar:3];.
  • : , , foo.bar [foo bar].

- - . , (foo.doSomething), , . , , , . , , .

+4

, . Objective C () [instance message]. , , , , . , :

self.display.text = [self.display.text stringByAppendingFormat:digit];
[[self display] setText:[[[self display] text] stringByAppendingFormat:digit]];

, ByAppendingFormat . - , .

+3

, clang /, self.display [self display] . , Objective-C 2.0. .

, - , ( ) :

self.display.text.stringByAppendingFormat:digit;

, , , .

+2

, , Objective C. :

NSString *s = @"Hello World!";
NSLog(@"Length is %d", s.length);

, . Objective C id. :

id s = @"Hello World!";
NSLog(@"Length is %d", s.length);

, id , length. :

id s = @"Hello World!";
NSLog(@"Length is %d", [s length]);

, Objective C NSString, , , length. , :

id s = [[UIView alloc] init];
NSLog(@"Length is %d", [s length]);

, (unrecognized selector sent to instance), UIView length.

+2

, Class variableOne, .

- . , , , . Class.variableOne... variableOne Class "." , , - , .

. ...

-(int) setVariable:x {
    self.variableOne = x;
}

-(int) showVariable {
    return self.variableOne
}

, [variableOne setVariable:5] [variableOne showVariable], .

This is a very simple way to think about the difference, I understand that another answer has already been accepted, but perhaps this answer will explain this to someone who does not understand the other answer.

+2
source

All Articles