About the KVC method
-(void) SetValue: (nullable ID) value Forkey: (NSString *) key;
As can be seen from the parameter type above, value must be an object and can be a nil object.
When your attribute is @property (nonatomic, assign) Nsinteger testnum;
When you use KVC, you must convert it to a NSNumber object, or to a NSString object. But you cannot convert other object types such as an array.
[Self setvalue:@99 forkey:@ "Testnum"]; √
[Self setvalue:@ "forkey:@" "Testnum"]; √
[Self setvalue:@[@99] forkey:@ "Testnum"]; X. program crashes, hint:-[__nsarrayi Longlongvalue]: Unrecognized selector sent to instance 0x12750c110
Guessing: When the type of the property is the underlying type, using KVC, Apple internally sends an-longlongvalue message to the incoming value parameter and assigns the value to the property. Nsarray, however, did not longlongvalue this method and collapsed. (both NSNumber and NSString have longlongvalue methods).
Example:
[Self setvalue:@ "forkey:@" "Testnum"];
NSLog (@ "%ld", self.testnum); The print here is 99.
Self.testnum + = 4;
NSLog (@ "%ld", self.testnum);
Note: What type of attribute are you declaring, and when you use KVC, the parameter that is passed in value should be the corresponding type. Otherwise you will crash if you use this property, especially if you invoke the method inside the property type.
For example: There are attributes @property (nonatomic, strong) NSString *teststr;
The following code is available:
Nsarray *SD = @[@ "AFAF"];
[Self setvalue:sd forkey:@ "teststr"];
NSLog (@ "%@", self.teststr); Print: (AFAF). Printing the value of this property does not crash.
NSString *d = self.teststr; There will be no crashes and no warnings.
NSLog (@ "%@", [D class]); Printed: __nsarrayi. Description The type of variable d is not nsstring.
NSString *gs = [NSString stringwithstring:d]; The program crashes in this line. Hint-[__nsarrayi length]: Unrecognized selector sent to instance 0x15cd2c1d0
NSLog (@ "%@", GS);
Other than that:
#if __lp64__ | | (target_os_embedded &&!) Target_os_iphone) | | Target_os_win32 | | Ns_build_32_like_64
typedef long Nsinteger;
typedef unsigned long Nsuinteger;
#else
typedef int Nsinteger;
typedef unsigned int nsuinteger;
#endif
For the NSNumber class, it has the following methods
@property (readonly) int intvalue;
@property (readonly) unsigned int unsignedintvalue;
@property (readonly) long longvalue;
@property (readonly) unsigned long unsignedlongvalue;
@property (readonly) long long longlongvalue;
@property (readonly) unsigned long long unsignedlonglongvalue;
@property (readonly) Nsinteger IntegerValue ns_available (10_5, 2_0);
@property (readonly) Nsuinteger unsignedintegervalue ns_available (10_5, 2_0);
For the NSString class, it has:
@property (readonly) int intvalue;
@property (readonly) Nsinteger IntegerValue ns_available (10_5, 2_0);
@property (readonly) long Long Longlongvalue ns_available (10_5, 2_0);
As you can see, the method of converting NSString objects into basic types within the NSString class is much less than in the NSNumber class. So when you have attributes for
@property (nonatomic, assign) Nsuinteger testnum;
Then when the following KVC is executed, it crashes. Tip:-[__nscfconstantstring Unsignedlonglongvalue]: Unrecognized selector sent to instance 0x1001008d0
[Self setvalue:@ "forkey:@" "Testnum"];
and execute self setvalue:@99 forkey:@ "Testnum"]; it will not crash.
In summary, when modeling, if no special requirements, the base data type is best set to Nsinteger, or int or simply set to NSString type. Setting to Nsuinteger may cause crashes when using KVC. Especially when combined with Jsonmodel third-party libraries.
KVC-(void) SetValue: (nullable ID) value Forkey: (NSString *) key;