使用 AnyObject 时 Bool 被视为 int [英] Bool being seen as int when using AnyObject

查看:15
本文介绍了使用 AnyObject 时 Bool 被视为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Swift iOS 应用程序中使用 Dictionary<String,AnyObject> 类型的参数列表来保存一些最终将传递给 Web 服务的参数.如果我要将 Bool 保存到该字典然后打印它,它会显示为Optional(1)",因此它正在转换为 int.我不能发送一个 int 并且需要它是真实的".示例代码如下.

var params = Dictionary()参数 [测试者"] = trueprintln(params["tester"])

如何将其保存为实际的真/假值而不是整数?

注意事项

这与 AFNetworking 一起使用,因此参数必须采用 AnyObject!

形式

AFNetworking结构如下:

manager.GET(<#URLString: String!#>, 参数: <#AnyObject!#>, 成功: <#((AFHTTPRequestOperation!, AnyObject!) -> Void)!##(AFHTTPRequestOperation!, AnyObject!) -> Void#>, 失败:<#((AFHTTPRequestOperation!, NSError!) -> Void)!##(AFHTTPRequestOperation!, NSError!) -> Void#>)

参数"参数是我在 Dictionary<String,AnyObject> 中传递的内容.

解决方案

代替:

var params = Dictionary()

试试:

var params = Dictionary()

<块引用>

Any 可以表示任何类型的实例,包括函数类型.

文档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_448p>

在这种情况下,您似乎需要一个 Dictionary 并且该服务期望true"而不是 bool 值.

我建议创建一个函数来将您的布尔值转换为字符串并使用它来设置您的参数["tester"].

示例:

param["tester"] = strFromBool(true)

然后定义函数 strFromBool 接受一个 bool 参数并根据其值返回true"或false".

I am using a list of params of type Dictionary<String,AnyObject> in a Swift iOS application to hold some parameters that will eventually be passed to a webservice. If I were to save a Bool to this dictionary and then print it, it appears as "Optional(1)", so it is being converted to an int. I cannot send an int and need this to be "true". Sample code below.

var params = Dictionary<String,AnyObject>()
params["tester"] = true
println(params["tester"])

How can I get this to save as an actual true/false value and not as an integer?

Caveats

This is being used with AFNetworking, so the parameters are required to be of the form AnyObject!

The AFNetworking structure is as below:

manager.GET(<#URLString: String!#>, parameters: <#AnyObject!#>, success: <#((AFHTTPRequestOperation!, AnyObject!) -> Void)!##(AFHTTPRequestOperation!, AnyObject!) -> Void#>, failure: <#((AFHTTPRequestOperation!, NSError!) -> Void)!##(AFHTTPRequestOperation!, NSError!) -> Void#>)

the 'parameters' argument is what I am passing in the Dictionary<String,AnyObject> as.

解决方案

Instead of:

var params = Dictionary<String,AnyObject>()

Try:

var params = Dictionary<String,Any>()

Any can represent an instance of any type at all, including function types.

Documentation: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_448

In this case it appears you need a Dictionary and the service is expecting "true" as opposed to the bool value.

I recommend creating a function to convert your bool value to a String and using that to set your params["tester"].

Example:

param["tester"] = strFromBool(true)

and then define the function strFromBool to accept a bool parameter and return "true" or "false" depending on its value.

这篇关于使用 AnyObject 时 Bool 被视为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆