iOS knowledge summary

First, the difference instancetype and id

1、instancetype在类型表示上,跟id一样,可以表示任何对象类型
2、instancetype只能用在返回值类型上,不能像id一样用在参数类型上
3、instancetype比id多一个好处:编译器会检测instancetype的真实类型
注:作为返回值时,凡是用id的地方,都建议换成instancetype

Second, the nature of the property

@property = ivar + getter + setter;
instance variables + setter method + setter methods, i.e. using the system will automatically generate @property member variables, the setter and getter methods;

Three, @ synthesize and @dynamic respectively, what role

  • @property two corresponding words are @synthesize and @dynamic. If @synthesize and @dynamic did not write, the default is @syntheszie var = _var;
  • @synthesize semantics if you do not have to manually implement getter and setter methods method, the compiler will automatically as you add these two methods.
  • @dynamic tells the compiler: setter and getter methods implemented by the user's own attributes, not automatically generated. (Of course, for readonly attributes can only provide getter). If a property is declared as @dynamic var, then you do not provide @setter method and @getter method, compile time no problem, but when the program runs to instance.var = someVar, due to the lack setter methods will cause the program to crash; or when when you run the someVar = var, due to the lack getter method will also lead to a crash. No compile-time problem, the appropriate method of execution until runtime, this is called dynamic binding.

Four, atomic

intended to refer to atomic access method attributes are thread-safe, to ensure that the entire object is not thread-safe, multiple threads, only guarantee atomic getter, setter method is safe, does not guarantee that other operations, such as string concatenation, remove array elements, etc., and did not execute getter and setter methods, Gu is not absolutely safe.

Five, Block nature

  • OC is essentially a block on an object, it also has an internal pointer isa
  • block is the object encapsulates the OC function calls and function calling environment
  • is a wrapper function block and context objects OC
3066088-1f0f23fe32523bce.png
image.png
  • auto variable block access method is passed by value, auto automatic variable might be destroyed, the memory may disappear, do not use the pointer to access;
  • static variable block access method is a pointer passed, static variables are kept in memory, the pointer can be accessed;
  • block does not need to capture global variables, are directly using the value of


    3066088-d16cb90dfdc005e7.png
    image.png
  • block will be caught in access self: self is when the call parameters of function block parameters are local variables, self pointing to the caller
  • block in the access member variables can be captured: Access member variable is actually self-> xx, to capture self, and through self access inside the member variables

block of three types:

1、__NSGlobalBlock __ 在数据区 :没有访问auto变量的block是__NSGlobalBlock __ ,放在数据段;调用copy操作后,什么也不做
2、__NSMallocBlock __ 在堆区 : [__NSStackBlock __ copy]操作就变成了__NSMallocBlock __ ;复制效果是:引用计数增加,副本存储位置是堆
3、__NSStackBlock __ 在栈区 : 访问了auto变量的block是__NSStackBlock __ ;调用copy操作后,复制效果是:从栈复制到堆;副本存储位置是堆

In ARC environment, the compiler will look at several cases automatically copy block on the stack to the heap:

. 1, when the return value as a function of block
2, when the block assigned to a pointer __strong
time method. 3, a block containing the method name Cocoa API parameters usingBlock
time. 4, block parameters as a method of GCD API

When the internal block access to the object type auto variables:

If the block stack space, regardless of external variables is strong references or weak, block will be weak reference to access the object
if the block in the heap, if strong external reference, internal block is strong references; if external weak reference, internal block is weak.
Stack block:
A) If the block is on the stack, will not have a strong pair of auto variable references
b) block on the stack will be destroyed at any time, did not need to go strong reference other objects
heap block:
1. If the block is copied to stack:
a) calls inside Block copy function
b) internal copy function calls the function _Block_object_assign
c) _Block_object_assign function will make a corresponding operation according modifiers auto variable (__strong, __ weak, __ unsafe_unretained ), forming strong references ( the retain) or weak reference
2. If the block is removed from the heap
a) inside the block calls the function dispose
b) an internal function call _Block_object_dispose dispose function
c) _Block_object_dispose function automatically release a reference auto variables (release)

__block modifier effect:

  • __block can be used to resolve internal block variable values ​​can not be modified auto problems
  • __block can not modify global variables, static variables (static)
  • The compiler will be packaged as an object variable __block
  • __block modifying variables: age -> __ forwarding-> age
  • __Block_byref_age_0 internal structure and external variables age address is the same address

Six, NSCache and NSMutableDictionary similarities and differences:

The same point:
NSCache NSMutableDictionary function and usage is basically the same.
Difference:
NSCache is thread-safe, NSMutableDictionary thread safe
NSCache thread-safe, thread Mutable classes are generally developed unsafe
NSCache will automatically free up memory when out of memory (so take the time data from the cache always judge is empty)
NSCache quota can be specified cache, when the cache memory exceeds the limit automatically release the
cache limit:
the number of cache
@property NSUInteger countLimit;
cache cost
@property NSUInteger totalCostLimit;
apple to NSCache packaging methods and properties more than the NSMutableDictionary to many powerful features

Seven, runtime by selector are two ways to find an address corresponding to IMP

method one:

类方法(假设有一个类 A)
class_getMethodImplementation(objc_getMetaClass("A"),@selector(methodName));
实例方法
class_getMethodImplementation([A class],@selector(methodName));

Second way:

类方法
Method class_getClassMethod(Class cls, SEL name)

实例方法
Method class_getInstanceMethod(Class cls, SEL name)

最后调用IMP method_getImplementation(Method m) 获取IMP地址

Eight, avoid setting rounded cause offscreen rendering optimization

Method 1: Bezier UIBezierPath draw frame and a rounded Core Graphics

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];       
imageView.image = [UIImage imageNamed:@"myImg"];        
//开始对imageView进行画图        
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);        
//使用贝塞尔曲线画出一个圆形图        
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];       
 [imageView drawRect:imageView.bounds];       
imageView.image = UIGraphicsGetImageFromCurrentImageContext();       
//结束画图        
UIGraphicsEndImageContext();       
[self.view addSubview:imageView];

Second way: using CAShapeLayer rounded and provided UIBezierPath

 UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,100,100,100)];
 imageView.image = [UIImage imageNamed:@"myImg"];
 UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
 CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
 //设置大小
 maskLayer.frame=imageView.bounds;
 //设置图形样子
 maskLayer.path=maskPath.CGPath;
 imageView.layer.mask=maskLayer;
 [self.view addSubview:imageView];

Nine, using ShadowPath specified layer shadowing effects path to avoid off-screen rendering

imageView.layer.shadowColor=[UIColor grayColor].CGColor;        
imageView.layer.shadowOpacity = 1.0;        
imageView.layer.shadowRadius = 2.0;        
UIBezierPath *path = [UIBezierPath bezierPathWithRect:imageView.frame]; 
imageView.layer.shadowPath = path.CGPath;

Reproduced in: https: //www.jianshu.com/p/caa4e6b5abad

Guess you like

Origin blog.csdn.net/weixin_33873846/article/details/91254029