【问题标题】:How to return a JSONObject as a String in swift?如何快速将 JSONObject 作为字符串返回?
【发布时间】:2016-04-26 03:37:08
【问题描述】:

我想创建一个名为RegisterInnew object,对象的目标是生成一个json对象作为字典并作为字符串返回

这是我的代码

public class RegisterIn {

    private var a : String = ""         //required
    private var b: String = ""          //required
    private var c: String = ""          //required
    private var d: Int =  0             //required

    private let BD_a : String = "a"
    private let BD_b : String = "b"
    private let BD_c : String = "c"
    private let BD_d : String = "d"

    init(a: String, b: String, c: String, d: Int) {
        self.a = a
        self.b = b
        self.c = c
        self.d = d
    }

    func getJSONObject() {
        let jsonDic : [String: AnyObject] = [
            BD_a: a,
            BD_b: b,
            BD_c: c,
            BD_d: d
        ]
        do {
            let jsonObject = try NSJSONSerialization.dataWithJSONObject( jsonDic, options: NSJSONWritingOptions.PrettyPrinted)
        } catch let error as NSError {
            print(error)
        }
    }

    func toString() {
        return String(getJSONObject())      <- this line occur error
    }
}

在函数getJSONObject,我认为它返回一个jsonObject as [String: AnyObject]。在我的ViewController,我想把它分配给Label.text,它总是

我的ViewController:

@IBOutlet weak var jsonLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let a = RegisterIn.init(a: "123", b: "456", c: "789", d: 00000)

    jsonLabel.text = a
} 

我想我必须更改 RegisterIn 类中的一些代码,真的需要一些帮助!

【问题讨论】:

    标签: ios json string swift


    【解决方案1】:

    你从来没有从getJSONObject()返回一个字符串,试试

    func getJSONObject() -> String? {
        let jsonDic : [String: AnyObject] = [
            BD_a: a,
            BD_b: b,
            BD_c: c,
            BD_d: d
        ]
        do {
            let jsonObject = try NSJSONSerialization.dataWithJSONObject( jsonDic, options: NSJSONWritingOptions.PrettyPrinted)
    
            return NSString(data: jsonObject, encoding:NSUTF8StringEncoding)
    
        } catch let error as NSError {
            print(error)
            return nil
        }
    }
    
    func toString() {
        return getJSONObject() //to be more correct, but this function is sort of redundant, just call getJSONObject directly, but up to you  whats the best
    }
    

    【讨论】:

    • 这只是问题的一半。 jsonLabel.text = a 如果是另一半。
    • 啊,对,我猜只需要jsonLabel.text = a.toString(),但我觉得奇怪的是它实际上是在调用函数,也许 toString 隐含了一些快速的魔法
    • 你不是说jsonLabel.text = a.getJSONObject()吗?
    • 顺便说一句 - getJSONObject 是一个返回字符串的函数的可怕名称。
    • 非常感谢!真的很有帮助。
    【解决方案2】:

    可能是

    "let jsonObject = try NSJSONSerialization.dataWithJSONObject( jsonDic, options: NSJSONWritingOptions.PrettyPrinted)"
    

    有问题。 您可以使用null 来替换PrettyPrinted"options:[])"

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多