Dark Horse programmer--oc Language learning experience--attribute declaration and assignment

Source: Internet
Author: User

Dark Horse Programmer--oc Language learning experience-- attribute declarations and Assignments

-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------

1, all classes in OC inherit from the ultimate parent class object
2, the declaration of a character variable with n is a string  *_xxx instance variable, usually preceded by an underscore
3, in OC, the method is distinguished by the +-number- The number beginning with the instance method or object method   + is the class method    pre-used object called by the latter with the class name called

5, Property assignment is available. The way or the pointer is assigned to the value

    1. @interface Person:nsobject
    2. {
    3. int _pid;
    4. NSString *_name;
    5. int _age;
    6. NSString *_email;
    7. NSString *_address;
    8. }
    9. Get Set method
    10. -(void) Setpid: (int) PID;
    11. -(int) returnpid;
    12. A simple method to implement the method of attribute declaration automatically Setget
    13. @property (nonatomic) int pid;//nonatomic represents non-atomic, non-thread-safe atomic represents thread safety
    14. @property NSString *name;
    15. @property int age;
    16. @property NSString *email;
    17. @property NSString *address;
    18. @end
Copy Code


    1. @implementation person
    2. Assign value
    3. -(void) Setpid: (int) pid{
    4. Access by pointer
    5. self->_pid=pid;
    6. }
    7. Take value
    8. -(int) returnpid{
    9. Wrong wording Self->return PID
    10. Return self->_pid;
    11. }
    12. The implementation of the property is then used directly. Call
    13. @synthesize Name=_name;
    14. @synthesize Age=_age;
    15. @synthesize Email=_email;
    16. @synthesize address=_address;
Copy Code


  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. #import "Person+ext.h"
  4. int main (int argc, const char * argv[]) {
  5. @autoreleasepool {
  6. Insert code here ...
  7. NSLog (@ "First function!!");
  8. Create an instance
  9. Person *per=[[person Alloc]init];
  10. Call yourself to write the Setpid method to assign a value
  11. [Per setpid:1234567];
  12. Call the value method Returnpid return the new value
  13. int Pid=[per Returnpid];
  14. NSLog (@ "pid=%d", PID);
  15. Use the. Call implementation to assign a value
  16. [Email protected] "Zhinzik";
  17. per.age=20;
  18. [Email protected] "xxxxxxx";
  19. [Email protected] "Chengdu";
  20. Using the. Call implementation value '
  21. NSString *name=per.name;
  22. int age=per.age;
  23. NSString *email=per.email;
  24. NSString *address=per.address;
  25. NSLog (@ "%@\n%d\n%@\n%@", name,age,email,address);
  26. [Per Test];
  27. }
  28. return 0;
  29. }
Copy Code

-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------

Dark Horse programmer--oc Language learning experience--attribute declaration and assignment

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.