当在字典中设置nil值时,它会崩溃。 以下是代码。
NSMutableDictionary *dic = [@{} mutableCopy];
[dic setObject:nil forKey:@"test"];
此代码将崩溃。那么有没有办法覆盖setObject:forKey:使nil传递给它时不会崩溃,就像这样:[dic setObject:nil forKey:@"test"];
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
如果您仅使用<div class="row">
<div class="scrollablediv">
<section class="small-2 small-offset-8 columns">
<img src="img/blanche.png" alt="Blanche" />
<p>Praesent dui sapien, pulvinar ut velit eu, vulputate viverra purus. Suspendisse aliquet, enim cursus luctus luctus, mauris massa pretium nisl, vulputate aliquam risus arcu et erat. Donec varius nisl sit amet neque facilisis eleifend. Donec ut elit eu enim scelerisque malesuada. Quisque finibus leo et est volutpat, a vestibulum enim tempor. Nam pharetra et leo sed venenatis. Maecenas in erat tincidunt, dignissim mauris nec, rhoncus mi. Suspendisse venenatis bibendum ex quis luctus. In vel erat at massa scelerisque pulvinar ac a urna. Nunc mauris nisi, mollis eu ante eget, bibendum tempus dolor. Aliquam porttitor, dolor vel egestas venenatis, mauris erat pretium augue, ac dictum nunc augue eu elit. Curabitur at dictum elit, id ultrices quam. Mauris hendrerit risus id cursus iaculis. Morbi quis justo arcu.</p>
</section>
<section class="small-2 columns">
<img src="img/nuit.png" alt="Nuit" />
<p>Praesent dui sapien, pulvinar ut velit eu, vulputate viverra purus. Suspendisse aliquet, enim cursus luctus luctus, mauris massa pretium nisl, vulputate aliquam risus arcu et erat. Donec varius nisl sit amet neque facilisis eleifend. Donec ut elit eu enim scelerisque malesuada. Quisque finibus leo et est volutpat, a vestibulum enim tempor. Nam pharetra et leo sed venenatis. Maecenas in erat tincidunt, dignissim mauris nec, rhoncus mi. Suspendisse venenatis bibendum ex quis luctus. In vel erat at massa scelerisque pulvinar ac a urna. Nunc mauris nisi, mollis eu ante eget, bibendum tempus dolor. Aliquam porttitor, dolor vel egestas venenatis, mauris erat pretium augue, ac dictum nunc augue eu elit. Curabitur at dictum elit, id ultrices quam. Mauris hendrerit risus id cursus iaculis. Morbi quis justo arcu.</p>
</section>
</div>
</div>
个实例作为键,则可以安全地使用NSString
中的下一个类别:
NSKeyValueCoding.h
如果要将其他类型用作键,或者在key为nil时避免异常,则可以为NSMutableDictionary创建自己的类别。例如:
@interface NSMutableDictionary<KeyType, ObjectType>(NSKeyValueCoding)
/* Send -setObject:forKey: to the receiver, unless the value is nil, in which case send -removeObjectForKey:.
*/
- (void)setValue:(nullable ObjectType)value forKey:(NSString *)key;
@end
请注意,无法覆盖 @interface NSMutableDictionary (CocoaFix)
- (void)setObjectOrNil:(id)object forKeyOrNil:(id)key;
@end
@implementation NSMutableDictionary (CocoaFix)
- (void)setObjectOrNil:(id)object forKeyOrNil:(id)key {
if (object && key) {
[self setObject:object forKey:key];
}
}
@end
的原始setObject:forKey:
方法。通常,可以使用类别中的方法调配来执行此操作。但NSMutableDictionary
实际上是类集群。 NSMutableDictionary
的私有子类完成了真正的工作。每个子类都有自己的NSMutableDictionary
实现,因为这个方法是原始的。您可以在https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html
答案 1 :(得分:1)
您无法从NSDictionary的文档中获取:
通常,密钥可以是任何对象(前提是它符合 NSCopying协议 - 见下文),但请注意使用键值时 编码密钥必须是一个字符串(参见键值编码基础)。 密钥和值都不能为零;如果你需要表示null 字典中的值,你应该使用NSNull。
答案 2 :(得分:0)
无论如何你想逃脱崩溃。对?为此,您应该在try-catch块中编写代码
@try {
NSMutableDictionary *dic = [@{} mutableCopy];
[dic setObject:nil forKey:@"test"];
}
@catch (NSException *exception) {
NSLog(@"%@",exception.reason);
}
@finally {
}
//你可以这样做
NSNull *nullValue=[[NSNull alloc]init];//globally decalare
NSMutableDictionary *dic = [@{} mutableCopy];
[dic setObject:dic?dic:nullValue forKey:@"test"];