gpt4 book ai didi

json - 在为我的 UITextField 展开可选值时意外发现 nil

转载 作者:行者123 更新时间:2023-11-28 13:11:13 24 4
gpt4 key购买 nike

由于这个解包错误,我无法将 JSON 值(我正在成功读取)传递到我的 textfield 下一个 viewcontrollernil

我很困。这是我读取 JSON 的类:

class DoOAuth
{
func doOAuthFitbit() -> String{
var name = ""
let oauthswift = OAuth1Swift(
consumerKey: "eabf603efe9e45168d057b60b03f8e94",
consumerSecret: "46b4dfa8c9d59666769e03f887d531a8",
requestTokenUrl: "https://api.fitbit.com/oauth/request_token",
authorizeUrl: "https://www.fitbit.com/oauth/authorize?display=touch",
accessTokenUrl: "https://api.fitbit.com/oauth/access_token")

oauthswift.authorizeWithCallbackURL( NSURL(string: "fitbit://oauth")!,
success:{
credential, response in

let vc: ViewController = ViewController()


let user: OAuthSwiftClient = OAuthSwiftClient(consumerKey: oauthswift.consumer_key, consumerSecret: oauthswift.consumer_secret, accessToken: credential.oauth_token, accessTokenSecret: credential.oauth_token_secret)

let object:[String : AnyObject] = ["oauth_token": credential.oauth_token, "oauth_token_secret" : credential.oauth_token_secret]

user.get("https://api.fitbit.com/1/user/-/profile.json", parameters: object,
success: {

(data: NSData, response: NSHTTPURLResponse) -> Void in
let jsonValues = JSON(data: data, options: NSJSONReadingOptions.AllowFragments, error: nil)
println(jsonValues)

/*public var dictionary: [Swift.String: JSON]?
{
switch self
{
case .Dictionary(let d):
var jsonObject: [Swift.String: JSON] = [:]
for(k,v) in d
{
jsonObject[k] = JSON.wrap(v)
}
return jsonObject
default:
return nil
}
}*/

for(key, subJson) in jsonValues
{
if let nm = subJson["fullName"].string
{
println("\(nm)")

name = nm
}
}

/*for(index: String, subJson: JSON) in jsonValues
{
let name = subJson.dictionary?["fullName"]?.string

println("\(name!)")
//vc.nm.text = name!
main.acceptJson(name!)

}*/
},
failure: {
(error:NSError!) -> Void in
println(error.localizedDescription)
println("error")
})
},
failure: {
(error:NSError!) -> Void in
println(error.localizedDescription)
})
return name
}

我调用了一个函数,该函数应该接收位于下一个 View Controller 中的 JSON 字符串 (acceptJson):

class mainMenu: UIViewController
{
var oauthfitbit: DoOAuth = DoOAuth()
var name = ""

//let vc: ViewController = ViewController()

@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var nameField: UITextField!{
didSet{
nameField.text = name
}
}



override func viewWillAppear(animated: Bool)
{
//name = oauthfitbit.doOAuthFitbit()
//self.nameField.text = "Working"
//self.nameField.text = name
}

func acceptJson(info: String!)
{
println("\(info)")
self.nameField.text = info
//name = info

}

}

我在设置 nameField.text 行上抛出异常,说明 nameFieldnil。如何让 textfield 存储 JSON 字符串?
这是初始的 View Controller :

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBOutlet weak var nm: UITextField!
//let main: mainMenu = mainMenu()

var name = ""
@IBAction func connectPressed(sender: UIButton)
{
var oauthFitbit: DoOAuth = DoOAuth()
name = oauthFitbit.doOAuthFitbit()
self.performSegueWithIdentifier("loginSuccess", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "loginSuccess")
{
let controller = segue.destinationViewController as! mainMenu
controller.name = name
//vc.nameField.text = "Hello"

}
}

最佳答案

您是如何创建文本字段的?是通过 Interface Builder 实现的吗?在使用 Interface Builder 和 IBOutlets 时,我遇到过很多次这类问题。

第一步是确保您的文本字段从 .xib 文件正确连接到您的 View Controller 。删除连接并通过控制(命令?)重新连接,从 IB 拖动到您的 View Controller 代码。

如果您没有使用 IB 但仍有问题,请将代码发布到您创建文本字段的位置。我相信,如果您以编程方式创建它,则必须将 View Controller 设置为文本字段委托(delegate)。我已经有一段时间没有这样做了。

让我们知道!

关于json - 在为我的 UITextField 展开可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com