Crash protection in iOS (ii) KVC caused by crash

Source: Internet
Author: User

?? "Crash protection in iOS (i) unrecognized selector sent to instance" We overcame the crash that could not find a way to achieve, this one I mainly analyze in KVC common crash. and protective measures.

[Object Setvalue:nil Forkey:key]

?? Value is nil. This method is called when key is not nil, and -(void)setNilValueForKey:(NSString *)key I rewrite the method here, such as the following code:

-(void)setNilValueForKey:(NSString *)key{    NSString *crashMessages = [NSString stringWithFormat:@"JKCrashProtect:‘NSInvalidArgumentException‘, reason: ‘[%@ %p setNilValueForKey]: could not set nil as the value for the key %@.‘",NSStringFromClass([self class]),self,key];    [[JKCrashProtect new] JKCrashProtectCollectCrashMessages:crashMessages];}

Test code:

 NSObject *objectnew];    [object setValue:nil forKey:@"name"];

The results of the execution are as follows:

2017-05-0419:14:14.073 JKCrashProtect_Example[28537:40288150x600000003b30classisnotforthename,value:(null)‘

We can see that the program no longer produces crash, and will crash print information to facilitate our positioning crash.

Non-object property value is nil generated by crash

The instance code is as follows:

@interface Person : NSObject@property (nonatomic,copy)NSString *name;@property (nonatomic,assign)NSInteger age;@end

Test code:

setforKey:@"age"];

Assuming that we do not carry out crash protection, the above test code will produce crash, but after protection. The results of the execution are as follows:

2017-05-0421:19:52.496 JKCrashProtect_Example[29792:40653830x608000225560notsetastheforthe key age.‘

The program is not crash, and the crash information is printed out to facilitate our positioning.

Key is not a property of the object generated by crash

?? Assuming that key is not a property of object, we can also generate crash if we do the KVC assignment.


Test code such as the following:

 NSObject *objectnew];  [object setValue:@"abc" forKey:@"123"];

The results of the execution are as follows:

2017-05-0421:26:21.357 JKCrashProtect_Example[29942:40713850x600000203180classisnotforthe123,value:abc‘

The same program does not crash. and will crash print information out, convenient for us to locate.

[Object Setvalue:value Forkey:nil] generated crash

?? When key is nil, the program generates crash. Of course, here can be handled by method Swizzle, but this violates, Jkcrashprotect created the original intention: only in the crash generation, the relevant processing, no crash time. The program executes normally. As little as possible to produce performance losses. This in the static, the compiler will have a warning, generally this crash generated in the dynamic incoming key did not do non-empty processing, my side of the temporary no good solution. If you have the words, welcome to the message a discussion oh.

[Object Setvalue:value Forkeypath:keypath];

?? Assuming KeyPath is wrong, this time the program will also produce crash, my side of crash protection after. Test code such as the following:

 @interface country : nsobject @property(nonatomic, copy)NSString*name;@end @interface  Person: nsobject @property(nonatomic, copy)NSString*name;@property(nonatomic,Assign)NsintegerAge@property(nonatomic,Strong) Country *country;@end
Country *country = [Country new];    jack.country =country;    setValue:@"111"forKeyPath:@"abc.name"];

Here KeyPath I wrote a random. The results of the execution are as follows:

2017-05-0423:30:20.580 JKCrashProtect_Example[30774:4107964] demo JKCrashProtect:‘Terminating app due to uncaught exception ‘NSUnknownKeyException‘, reason: ‘0x6000004280c0thisisvaluefor the key: abc

The program is not crash. And crash information is also printed out to facilitate our positioning.

That's all you have to say today. Welcome to continue to pay attention to OH.
Demo Address
Cocoapod:

"JKCrashProtect"

Crash protection in iOS (ii) KVC caused by crash

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.