OC Language -04-OC language-core syntax

Source: Internet
Author: User

One, dot syntax

1> Basic Use

    • Point syntax is essentially a call to the Set method/get method

2> Use note

    • If it appears to the right of the assignment operator, it is converted to the Get method at execution time
    • If it appears to the left of the assignment operator, it is converted to the Set method when executed
    • You cannot use self with self in a set, get method, which can cause a dead loop.

Ii. Property and Synthesize keywords

1> @property

    1. Role

      ① 自动生成某个成员变量的set方法和get方法
    2. Use note

      ① 只能出现在@interface中② 可以使用逗号运算符为多个同类型的成员变量生成setter和getter③ 为了与点语法匹配,使用@property的成员变量不要以下划线开头④ @property可以同时完成@synthesize的任务⑤ @property默认生成的成员变量是@private类型的以下划线开头

2> @synthesize

    1. Role

      ① 自动生成某成员变量的setter和getter的实现② 可以指定要访问的成员变量的名字,通常是以下划线开头
    2. Use note

      ① 只能出现在@implimentation中② 可以使用都好运算符同时生成多个setter和getter的实现③ 若未指定要访问的成员变量,默认会访问生成@synthesize后名字一  样的成员变量④ 若访问的成员变量未找到,将会自动生成@private类型的以下划线  开头(或与@synthesize后名字一样的)的成员变量 

III. type of ID

1> Basic Meaning

    • Universal pointer that can point to any OC object
    • Can be understood as NSObject *

2> Use note

    • ID does not add a symbol * when defining a variable, which itself already contains

Iv. Construction Methods

1> effect

    • Used to initialize the object, returning an initialized object

2> about +new methods

    1. +new equivalent to +alloc method and-init method
    2. +alloc method and-init method

      ① +alloc方法1)返回值为id类型2)为对象分配内存空间,并返回该对象② -init方法1)返回值为id类型2)初始化当前对象,并返回初始化好的对象

3> steps to override the-init method

    1. Call Super's-init method

      ① 初始化父类中的成员变量和其他属性② 返回当前对象,并赋值给self③ 根类将isa指针初始化为最初调用init方法的类
    2. Initializing a subclass member variable
    3. Returns the Initialized object

4> Custom-init method

    1. Applicable occasions

      ① 当初始化成员变量时,需要将成员变量初始化不同的值,通常需要自定义-init方法,将指定的值作为-init方法的参数
    2. Specification

      ① 一定是对象方法,以-开头② 返回值一定是id类型③ 方法名一定以init开头

V. Classification (category)

1> effect

    • Extend some methods for a class without changing the original class
    • Usually works with large classes, the different types of methods of the classes are written in different classifications

2> definition

    • The definition of a classification is similar to the definition of a class, with parentheses following the class name to indicate the name of the category
    • Classification is usually named by module

3> Use note

    • Classification can only be extended to a class and cannot be extended by member variables
    • member variables in the original class can be accessed in the implementation of the classification method
    • The method of implementing the original class in the classification will invalidate the implementation of the method in the original class.
    • When a method is called, it is first found in the taxonomy, then found in the original class, and finally in the parent class.
    • When more than one taxonomy of the original class finds a method, it is preferred to find it in the last compiled category

Six, NSString class

1> NSString * with char *

    1. NSString *

      NSString \*指向OC字符串对象的指针② NSString是OC字符串类封装了字符串相关操作的方法
    2. char *

      char \*创建的是指向C语言字符或字符串的指针② char \*只能存储字符指针或字符串指针,没有相关操作

2> NSString and Nsmutablestring

    • The string object created by NSString is immutable, and the string object created by Nsmutablestring can be changed

Methods of 3> NSString

  1. Two methods that a subclass of NSString must implement

    ① -length 对象方法,获取字符串的长度② -characterAtIndex: 对象方法,获取字符串指定索引位置的字符
  2. Initialize method

    ① +(NSString*)stringWithString: 类方法,创建一个字符串常量,在  Xcode6.0中已经没有该方法,用字符串常量直接创建② +(NSString*)stringWithFormat: 类方法,合成一个新的字符串③ -(NSString*)initWithString: 对象方法,在字符串对象初始化时,为其赋值一个字符串常量④ -(NSString*)initWithFormat: 对象方法,在字符串对象初始化时,为其赋值一个新合成的字符串
  3. Methods for reading strings in a file

    +(NSString*)stringWithContentsOfFile: encoding: error: 类方法
  4. Methods for getting strings in the network

    +(NSString*)stringWithContentsOfURL: encoding: error: 类方法
  5. Methods for string manipulation

    ① gets the substring of the specified range of the string-(NSString *) Substringwithrange: Object method,nsrange is struct type, return substring ② replace substring within specified range in string with specified string-(NSString *) Stringbyreplacingcharactersinrange:withstring: Object method, the string that returns the row ③ the range of substrings in the parent string-(nsrange) Rangeofstring: Object method, returning  Nsrange type ④ Compare two string contents-(BOOL) isequaltostring: (NSString *) Astring object method, return BOOL type ⑤ compare size of two strings-(  Nscomparisonresult) Compare: (NSString *) Astring object method, return nscomparisonresult enum type ⑥ split string with specified string-(Nsarray *) Componentsseparatedbystring: (NSString *) Separator object method, return nsarray array type pointer     

Vii. Methods of Description

1> Basic Use

    • , the corresponding description method is called
    • You can override the description method so that it can output the expected information through the class/object name

2> +description and-description

    1. +description method

      *类型
    2. -description method

      ① 对象方法,默认输出格式为<类名:内存地址>② 返回值为NSString *类型③ 不要在-description方法中用NSLog函数一%@格式输出self

Viii. Types of SEL

1> Basic Use

    1. Meaning

      ① SEL本质上是一个指针类型的数据② 保存的是方法的地址
    2. Role

      ① 将方法包装成SEL类型的数据② 每个对应一个唯一的SEL类型数据③ 通过SEL类型的数据可以间接调用类/对象方法

2> how the method is called indirectly through the SEL type

    1. Indirectly called by method name

      @selector(方法名),返回一个与参数对应的SEL类型的数据② performSelector:(SEL),通过SEL类型的数据间接的调用类/对象方  法③ performSelector:(SEL) WithObject:(id),通过SEL类型数据调用含  参数的类/对象方法
    2. Indirect invocation by method name in string form

      NSSelectorFromString: 将字符串类型的方法名转换成该方法对应的  SEL类型的数据② NSStringFromSelector: 将SEL类型的数据转换成字符串类型的方法  名③ 通过转化得到到SEL类型数据,再间接调用方法

Use of 3> _cmd

    • _cmd represents the SEL type data corresponding to the current method
    • Methods cannot be indirectly called through _cmd in a method, which causes a dead loop

Ix. examples

/* 1. Create a person class, properties: Name; 2. The custom Init method, in initialization, is the name 3 for each person object. Add a category containing the motion method to the person class with the name Sports 4. Overriding the-description method, When you make it output the person object, output the Name property of the object 5. Calling methods indirectly via SEL*/#import <foundation/foundation.h>/***** Define Person Class*****/@interface for person:nsobject//@propert declare member variables, and omit @synthesize@property NSString *name;/* Custom constructor method that assigns a value to the member variable name when the person object is initialized*/-(ID) initwithname: (nsstring*) name;//determine whether the names of two people are the same-(void) Issamename: (Person*) p;@end/**Implementation of the *person class***/@implementation person-(nsstring*) description{return self.name;} -(ID) Initwithname: (nsstring*) name{self.name = name; return self;} -(void) Issamename: (Person*) p{/* Call NSString Object method isequaltostring determine whether the names of two people are the same*/if ([Self.name isequaltostring:p.name]) {NSLog (@ "\ n These two names are:%@", self); }//By overriding the description method to output the object's properties through the object name NSLog (@ "\ n the names of these two persons are:%@\t%@", Self, p);}@end/**** Add a category to the person class****/@interface person (Sports)//Add table tennis to the person class this method-(void) playtabletennis;//for the person class to play basketball this method-(void) Playbasketball;@end/*The realization of classification sports of *person**/@implementation person (Sports)-(void) playtabletennis{NSLog (@ "\n%@ is playing table tennis", self);} -(void) playbasketball{NSLog (@ "\n%@ is playing basketball", self);}@end/***** test function Main **** */int Main () { @autoreleasepool {///define two strings to hold two names NSString * str = @ "Tom"; NSString *STR2 = @ "Jack";//define two person objects and invoke the overridden constructor method person Span class= "Hljs-keyword" >*p = [[Person alloc] initwithname:str]; Person *p2 = [[Person alloc] initwithname:str2];//Call method indirectly by method name [P Performselector: @selector (issamename:) WITHOBJECT:P2]; The method is called indirectly through the method name in string form [P performselector:nsselectorfromstring (@ "Playbasketball")];} return 0,                

OC Language -04-OC language-core syntax

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.