Objective-c class method Self keyword

Source: Internet
Author: User
NSString basic use

#import <Foundation / Foundation.h>

int main () {
    // The easiest way to create a string
    NSString * str = @ "大 武汉";
    NSLog (@ "我 在% @", str);
    
    // Another way to create a string
    int age = 15;
    int no = 5668;
    NSString * name = @ "黄 祎 a";
    NSString * newStr = [NSString stringWithFormat: @ "My age is% i and My no is% i and My name is% @", age, no, name];
    NSLog (@ "---------% @", newStr);
    
    // Get the length of the string
    int size = [name length];
    NSLog (@ "name length is% i", size);
    return 0;
}
Class method

#import <Foundation / Foundation.h>

@interface Person: NSObject
+ (void) test;
@end

@implementation Person
+ (void) test {
    NSLog (@ "hello world");
}
@end

int main () {
    [Person test];
    return 0;
}

/ *
 Object method
    Starts with-<only> callable by object
    Object methods can use member variables
    Relatively inefficient
 
 Class method
    Starts with + <only> can be called by class
    Class methods cannot use member variables
    Relatively efficient
 
 Object method name allowed in a class = class method name
 * /
self keyword

#import <Foundation / Foundation.h>

@interface Person: NSObject {
    @public
    int _age;
}
-(void) show;
-(void) test1;
-(void) test2;
@end

@implementation Person
-(void) show {
    NSLog (@ "Person's age is% i", _age);
}
-(void) test1 {
    int _age = 20;
    NSLog (@ "Person's age is% i", _age);
}
-(void) test2 {
    int _age = 30;
    NSLog (@ "Person's age is% i", self-> _ age);
}
@end

int main () {
    Person * p = [Person new];
    [p show];
    [p test1];
    [p test2];
    return 0;
}

// When the member variable and the local variable have the same name, the principle of proximity is used.
// Use self to access member variables Distinguish member variables from local variables

/ ** Application details ** /
// where it appears: all OC methods (object methods | class methods) cannot appear in functions
// Function: Use ‘self-> member variable name’ to access the member variables of the current method call; use ‘[self method name]’ to call the method (object method | class method)

/ ** Common errors ** /
// Use self to call the function
// Use self to call object method in class method Use self to call class method in object method
// self infinite loop
Objective-C class method self keyword
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.