当用户在我的应用程序中登录facebook时,某些预期值(如dateOfBirth,email)可能为零.我想将self.fbResponse.email的值设置为NSNull(),如果它是nil.这样我就可以将参数发布到服务器了.
let parameter:[String:AnyObject] =
[
"facebookId":self.fbResponse.fbID!,
"fbToken":"randomToken",
"email" : self.fbResponse.email!,
"dateOfBirth": self.fbResponse.dob,
"gender" : self.fbResponse.gender!,
"info": self.fbResponse.aboutMe!,
"fullName":self.fbResponse.fullName!,
"profileImageUrl":self.fbResponse.pictureURL!
"location": location
]
Run Code Online (Sandbox Code Playgroud) 我正在解析JSON响应并尝试检查我的一个键是否为空.我该怎么做?我有以下内容:
var routingNumber = (dic.value(forKey: "result") as! NSDictionary).value(forKey: "routingNumber") as! String
Run Code Online (Sandbox Code Playgroud)
这会返回:
无法将类型'NSNull'(0x107d238c8)的值转换为'NSString'(0x107329c40).
如何检查值是否为NSNULL?
if( something != NSNULL){
do something
}else{
do something else
}
Run Code Online (Sandbox Code Playgroud) 好.我搜索并搜索无济于事,我一直在打扰我.
我正在尝试确定UITextField是否具有空值,如果是,则将空值发送到服务器,而不仅仅是空字符串.
以下代码
NSDictionary *locationDict = @{
@"country":countryVal,
@"state":stateVal,
@"city":[cityField.text isEqualToString:@""] ? [NSNull null] : cityField.text
};
NSLog(@"%@", locationDict);
Run Code Online (Sandbox Code Playgroud)
记录这个
{
city = "<null>";
country = US;
state = TN;
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么要[NSNull null]登录"<null>".如果用户没有输入值,应用程序会继续将该字符串作为城市发送,显然不是我想要的.我没有在这里显示网络呼叫,但我正在使用AFNetworking来执行此操作.不过,在我调用AFN之前,我可以确认字典包含该字符串而不是null.所以我知道问题不在于AFN.
任何帮助是极大的赞赏.
我正在尝试使用NSArray不允许空值的NSDictionaries 的writeToFile方法.我想通过循环找到它们并用空NSString的替换,但我不能让它工作.这就是我正在尝试的.
编辑:NSArrays和NSDictionaries由NSJSONSerialization创建.
for (NSMutableDictionary *mainDict in mainArray)
{
for(NSMutableDictionary *subDict in mainDict)
{
for(NSString *key in subDict)
{
id value = [subDict objectForKey:key];
if (value == [NSNull null])
{
[subDict setObject:@"" forKey:key];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这引发了一个例外. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
但是你可以看到所有的NSDictionaries都是可变的.
这是怎么回事?有一个更好的方法吗?
编辑:将新版本移至单独的答案.
是[[NSNull null] isEqual:[NSNull null]] == YES?
测试id是否为NSNull的正确方法是什么.同样,如果检查两个集合是否相等并且它们都包含NSNull,它们是否仍然被认为是相等的?
我正在编写一个iOS应用程序,我需要通过Azure上的移动服务从SQL数据库中获取数据.
下载数据后,我得到了一个NSDictionary包含SQL-Table的所有属性.如果属性为空,则值为NSNull.
有没有一种方法可以NSNull在NSString没有IF语句的情况下传递给我(我不希望有20个if语句......)?
是否有一种快速方法可以确定字典中的值是否为NSNULL而不检查该对象类
[[dict objectForKey:@"foo"] class] != [NSNull null];
我有一个REST API,并且正在通过Json获取所有数据,然后将其放入IOS TableView中。我的问题是像NULLJson一样返回了一些数据
"vote_status":null
Run Code Online (Sandbox Code Playgroud)
我试图快速获取该NULL值并将其更改为字符串“ 0”,但是这样做很难。我到目前为止
if var vote_status = Stream["vote_status"] as? String {
if (vote_status == (vote_status)[NSNull]) {
vote_status = "0"
}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到这个编译错误:
无法使用索引类型为((NSNull).Type'(也称为'NSNull.Type')的索引对类型为'String'的值进行下标
我这样做是因为nil和null似乎不起作用,所以我不能这样做。
if (vote_status == nil) ...
Run Code Online (Sandbox Code Playgroud) 如何找到确切的位置 - [NSNull length]:发送到实例的无法识别的选择器我的意思是行号.
nsnull ×9
objective-c ×4
ios ×3
swift ×3
json ×2
nsdictionary ×2
cocoa-touch ×1
nsstring ×1
null ×1
string ×1
swift3 ×1