我很好奇,由于json-framework的帮助,我目前有NSDictionary一些值被设置为一个NSNull对象.
目的是剥离所有NSNull值并用空字符串替换它.
我确定有人在某处做过这件事吗?毫无疑问,这可能是一个四轮班轮,而且很简单,我只是太费力了,无法自己解决这个问题.
谢谢!
我想用+ [NSDictionary dictionaryWithObjectsAndKeys:]创建一个NSDictionary.我的一个键有一个字符串,但字符串有时可以.如果字符串是,我之后放的任何其他值键对都将被忽略,因为列表提前终止.处理a中可能存在价值的可能性的标准方法是什么?nilnilnilNSDictionary
这个问题类似于这个问题,但这种方法只适用于字典的根目录下.
我想NSNull用空字符串替换任何出现的值,以便我可以将完整的字典保存到plist文件(如果我用NSNull添加它,文件将不会写入).
但是,我的词典里面嵌套了词典.像这样:
"dictKeyName" = {
innerStrKeyName = "This is a string in a dictionary";
innerNullKeyName = "<null>";
innerDictKeyName = {
"innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary";
"innerDictNullKeyName" = "<null>";
};
};
Run Code Online (Sandbox Code Playgroud)
如果我使用:
@interface NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end
@implementation NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings {
const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self];
const id nul = [NSNull null];
const NSString *blank = @"";
for(NSString *key in replaced) …Run Code Online (Sandbox Code Playgroud) 我有一个项目/故事板场景在一个项目,直到昨晚工作在过去4周我工作正常.
我已经成功地评论了几乎所有内容,当我点击UITextField并键入数字时,我仍然会遇到崩溃.它只在我输入值时崩溃,否则它不会崩溃.
这是我正在运行它的类:
import Foundation
import UIKit
import HealthKit
import CoreData
class WorkoutViewController: UITableViewController {
//Properties
@IBOutlet var numberOfLapsTextField: UITextField?
@IBOutlet var metersPerLapTextField: UITextField?
@IBOutlet var workoutDurationTextField: UITextField?
@IBOutlet var paceTextField: UITextField?
var healthStore:HKHealthStore?
override func viewDidLoad() {
super.viewDidLoad()
}
Run Code Online (Sandbox Code Playgroud)
最初它有IBOutlets!代替 ?它具有CoreData堆栈属性,tableview背景的一些模糊背景效果,在viewDidLoad上从健康商店获取用户的权重,用于健康商店提取的谓词帮助方法以及取消和完成按钮.完成按钮从文本字段中捕获数据,进行一些计算并将数据保存到健康商店和coredata.但是所有这些都被注释掉了,只留下了上面所看到的.
我曾经设法在控制台中得到一个奇怪的堆栈跟踪(似乎不能再得到它)了:
[UIPhysicalKeyboardEvent _matchesKeyCommand:]
还有其他一些人喜欢它.
我难倒,有什么想法吗?异常断点是一个,但它只是将我引向AppDelegate类声明行,其中采用了UIResponder.我学习并重建了.我不知道还有什么可以寻找的.
我认为它是一个腐败的故事板文件,因为我已添加和删除文本字段的场景,他们都表现相同.这是我的storyboard.xml文件:
我有一个NSString对象,
NSString *aString;
Run Code Online (Sandbox Code Playgroud)
那么以下两个版本是等价的吗?
版本1:
if ( (NSString *)[NSNull null] == aString )
{
// Logic handling
}
Run Code Online (Sandbox Code Playgroud)
版本2:
if ( nil == aString )
{
// Logic handling
}
Run Code Online (Sandbox Code Playgroud)
我的简单测试结果表明上述两个版本有不同的行为:
何时aString初始化然后分配nil:
false 在版本1中表达,
true 在版本2中表达.
何时aString使用值初始化@"".
false 在版本1中表达,
false 在版本2中表达.
所以很明显,这两个版本的行为并不相同.
测试代码:
NSString *aString = nil;
NSString *bString = [NSString stringWithFormat:@""];
if ((NSString *)[NSNull null] == aString) {
NSLog(@"a1 - …Run Code Online (Sandbox Code Playgroud) 我有生辰八字的阵列阵列接收来自Facebook充满所以有一些朋友卫生组织生日是私人所以它包含NULL如何数组像空字符串转换,只要有空值的数组类似于下面
"<null>",
"10/29/1988",
"11/13",
"03/24/1987",
"04/25/1990",
"03/13",
"01/01",
"<null>",
"12/15/1905",
"07/10",
"11/02/1990",
"12/30/1990",
"<null>",
"07/22/1990",
"01/01",
"07/17/1989",
"08/28/1990",
"01/10/1990",
"06/12/1990",
Run Code Online (Sandbox Code Playgroud) 有时我会遇到JSON结构,例如“'data':null”。NSJSONSerialization将它们转换为NSNull,老实说,这很烦人。
我了解其背后的原因-诸如NSDictionary之类的内容不能包含nil值。
很好,但是在那种情况下,我宁愿看到反序列化数据结构中缺少的元素,这在许多情况下实际上简化了处理。是否有可以传递给NSJSONSerialization的选项或配置,并要求它完全做到这一点-只是错过了反序列化数据结构中的那些nil?
我经常发现需要缓存由NSJSONSerialization磁盘创建的数据结构,-writeToFile如果有空值则失败,我需要一个在结构未知时有效的修复.这是有效的,并且允许直接变异,因为NSMutableDictionary本身的实例没有被枚举,但感觉有点hacky.
这是完全正常还是重新创建一棵新树并返回它是绝对必要的?
- (void) removeNullsFromJSONTree:(id) branch
{
if ([branch isKindOfClass:[NSMutableArray class]])
{
//Keep drilling to find the leaf dictionaries
for (id childBranch in branch)
{
[self removeNullsFromJSONTree:childBranch];
}
}
else if ([branch isKindOfClass:[NSMutableDictionary class]])
{
const id nul = [NSNull null];
const NSString *empty = @"";
for(NSString *key in [branch allKeys])
{
const id object = [branch objectForKey:key];
if(object == nul)
{
[branch setObject:empty forKey:key];
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个PersonsArray: NSMutableArray = [NSNull, NSNull, NSNUll, NSNull, NSNull, NSNUll, NSNull].我需要七个插槽,然后我可以用EntObject CoreData条目填充AnyObject.
我需要在这个NSMutableArray上循环执行...
如果索引槽是NSNull我想传递给下一个索引槽,如果索引槽填充了我的对象我想在这个对象上执行代码.
example PersonsArray: NSMutableArray = [
NSNull,
NSNull,
NSNull,
"<iswift.Person: 0x7f93d95d6ce0> (entity: Person; id: 0xd000000000080000 <x-coredata://8DD0B78C-C624-4808-9231-1CB419EF8B50/Person/p2> ; data: {\n image = nil;\n name = dustin;\n})",
NSNull,
NSNull,
NSNull
]
Run Code Online (Sandbox Code Playgroud)
尝试
for index in 0..<PersonsArray.count {
if PersonsArray[index] != NSNull {println(index)}
}
Run Code Online (Sandbox Code Playgroud)
提出了一系列不起作用的变化,比如
if PersonsArray[index] as! NSNull != NSNull.self {println(index)}
Run Code Online (Sandbox Code Playgroud)
要么
if PersonsArray[index] as! NSNull != NSNull() {println(index)}
Run Code Online (Sandbox Code Playgroud)
注意:使用NSNull只是NSMutableArray中的占位符,因此它的计数总是7,我可以用Object替换任何(7)槽.我应该使用NSNull以外的东西作为我的占位符吗?
我已经创建了一个使用webservices的函数,现在我想检查if(id)对象是否为null.我在做什么在这里:
-(void)callService:(NSString*)request param:(NSString*) parameters completion:(void (^)(NSArray *list, NSError *error))completionHandler
{
[self.manager POST:request parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Response Object: %@", responseObject);
if (![responseObject isKindOfClass:[NSNull class]]) {
[self methodUsingJsonFromSuccessBlock:responseObject];
if (completionHandler) {
//NSLog(@"%@", list);
completionHandler(list,nil);
}
}
else {
NSLog(@"Nothing found");
}
}failure:^(NSURLSessionDataTask *task, NSError *error) {
//NSLog(@"Error: %@", [error description]);
if (completionHandler) {
completionHandler(nil,error);
}
}];
}
Run Code Online (Sandbox Code Playgroud)
但我在断点上找到的是当(id)为null时它有NSZeroData但没有NSNull所以它总是通过我的条件.我在用AFNetworking

在此先感谢帮助我.