WillKen's Blog.

Objective-C Notes

Word count: 918Reading time: 3 min
2019/09/16 Share

Objective C Notes

声明:本博客参考了易百教程菜鸟教程

  • 类的声明:@interface child : father

  • 方法的实现: @implementation child (不用制定父类)

  • .h文件中声明public methods .m文件中声明private methods

  • @interface 类名后加个括号:Objective-C 2.0中的Category语法 ,Category提供了一种比继承(inheritance)更为简洁的方法来对class进行扩展,我们可以为任何已经存在的class添加方法。

  • Defining Methods:

    1
    2
    -(void)learnUnit:(int)unit
    ofLang:(Language *)aLang;
  • @property (nonatomic) (nonatomic非线程安全)不需要再声明setter和getter函数。

  • @synthesize credit=_credit _id为@property存储变量名,约定:加下划线前缀。

  • strong为强引用,需要时给对象的内存不会被释放;weak是弱引用,随时可以被释放。

  • 中括号[类 方法] 语法,用来发消息。

  • 对于getter和setter可以使用 dot notation

  • nil

  • id is a type, which means “pointer to an object of any class”. id本身就是指针,id*是指向指针的指针。

  • 数据类型:NSString字符串

  • 数据类型:CGfloat 浮点值的基本类型

  • 数据类型:NSInteger 整型

    • 与int区别:在苹果api实现里,NSInteger是一个封装,它会识别当前操作系统的位数,自动返回最大的类型。

      typedef long NSInteger

      When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.**

      32程序:NSInteger相当于int32_t,4个字节为int的别名。

      64位程序:NSInteger相当于int64_t,8个字节为long long的别名。

      在兼容32位和64位系统时,int和long long比NSInteger可靠地多。

      因此在不考虑兼容32的时候:一般直接使用NSInteger就好了。

      在考虑兼容的时候,使用int和long long就好了。

  • 数据类型:BOOL 布尔型

  • 打印日志,NSlog(@"") 打印在设备日志和调试版本的控制台和分别调试模式上。

  • NSMutableArray 和 NSArray 是 ObjectiveC 中使用的数组类,前者是可变数组,后者是不可变数组。

    • 1
      2
      3
      4
      NSMutableArray *aMutableArray = [[NSMutableArray alloc]init];
      [anArray addObject:@"firstobject"];
      NSArray *aImmutableArray = [[NSArray alloc]
      initWithObjects:@"firstObject",nil];
  • NSMutableDictionary和NSDictionary是Objective中使用的字典,前者可变词典,后者不可变词典。

    • 1
      2
      3
      4
      NSMutableDictionary*aMutableDictionary = [[NSMutableArray alloc]init];
      [aMutableDictionary setObject:@"firstobject" forKey:@"aKey"];
      NSDictionary*aImmutableDictionary= [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:
      @"firstObject",nil] forKeys:[ NSArray arrayWithObjects:@"aKey"]];
  • 用extern可以创建外部文件可以访问的全局变量。使用extern关键字在任何地方声明变量。

  • 运算符基本与C++相同

  • 默认情况下,Objective-C编程语言使用按值调用方法来传递参数。

  • 要通过引用传递值,参数指针将像任何其他值一样传递给函数。 因此,需要将函数参数声明为指针类型。

  • 块是C,Objective-C和C++等编程语言中的高级功能,允许创建不同的代码段,这些代码段可以传递给方法或函数,就像它们是值一样。

    • 下面是一个简单的示例代码 -

      1
      2
      3
      4
      5
      6
      7
      double (^multiplyTwoValues)(double, double) = 
      ^(double firstValue, double secondValue) {
      return firstValue * secondValue;
      };

      double result = multiplyTwoValues(2,4);
      NSLog(@"The result is %f", result);
    • 这是一个在块中使用typedef的简单示例。 它是使用XCode运行的。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      #import <Foundation/Foundation.h>

      typedef void (^CompletionBlock)();
      @interface SampleClass:NSObject
      - (void)performActionWithCompletion:(CompletionBlock)completionBlock;
      @end

      @implementation SampleClass

      - (void)performActionWithCompletion:(CompletionBlock)completionBlock {

      NSLog(@"Action Performed");
      completionBlock();
      }

      @end

      int main() {

      /* 第一个Objective-C程序 */
      SampleClass *sampleClass = [[SampleClass alloc]init];
      [sampleClass performActionWithCompletion:^{
      NSLog(@"Completion is called to intimate action is performed.");
      }];

      return 0;
      }
      Objective-C

      执行上面示例代码,得到以下结果:

      1
      2
      2018-11-10 08:14:57.105 demo[184:323] Action Performed
      2018-11-10 08:14:57.108 demo[184:323] Completion is called to intimate action is performed.
  • 自定义类的init函数

    • 1
      2
      3
      4
      5
      6
      -(id)init {
      self = [super init];
      length = 1.0;
      breadth = 1.0;
      return self;
      }
  • Objective-C提供了类别和扩展

CATALOG
  1. 1. Objective C Notes