简体   繁体   English

Swift 3不能使用SwiftyJSON和Alamofire下标索引类型为'String'的'String'类型的值

[英]Swift 3 cannot subscript a value of type 'String' with an index of type 'String' using SwiftyJSON and Alamofire

This is the code, i was making a bitcoin price tracker app and im new to ios development so i am struggling with this error :( any help is appreciated. I am using Alamofire and SwiftyJSON to do the json work. They are imported using cocoapods. This is the json im using: https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD 这是代码,我正在制作一个比特币价格跟踪器应用程序,我是ios开发的新手,所以我正在努力解决这个错误:(任何帮助都很感激。我正在使用Alamofire和SwiftyJSON来做json工作。它们是使用cocoapods导入的这是json我使用的: https//apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD

// Connect the UI
@IBOutlet weak var PriceLabel: UILabel!
@IBOutlet weak var PercentageLabel: UITextView!
@IBOutlet weak var CurrencyPicker: UIPickerView!

var url : String = "https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD"

var Price : String = ""

var jsondata = ""


// Called before app is shown to user
override func viewDidLoad() {
    super.viewDidLoad()

    // Request the JSON Data
    requestJson()
}


func requestJson(){
    Alamofire.request(url).response { response in
        print("Request: \(response.request)")
        print("Response: \(response.response)")

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)")
            self.parseJSON(json: utf8Text)
        }
    }
}

func parseJSON(json: String){
    Price = jsondata["ask"].stringvalue **This is where it kicks an error**
}
func requestJson(){
    Alamofire.request(url).response { response in
        if let data = response.data {
            self.parseJSON(json: JSON(data: data))
        }
    }
}

func parseJSON(json: JSON){
    Price = json["ask"].stringValue 
}

for get json from URL you can also use responseJSON 要从URL获取json,您还可以使用responseJSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift-无法下标&#39;[Array]类型的值 <String> ]”,索引类型为“ IndexPath” - Swift - Cannot subscript a value of type '[Array<String>]' with an index of type 'IndexPath' Swift:无法为[String:NSObject]的下标值加上索引类型吗? - Swift: cannot subscript value of [String:NSObject] with index of type? 无法下标[AnyObject]的值? 索引类型为String - Cannot subscript a value of [AnyObject]? with an index of type String 不能使用索引类型(String,Int)Swift 2下标类型[(String,Int)]的值 - Cannot subscript a value of type [(String, Int)] with an index of type (String, Int) Swift 2 无法为索引类型为“ String!”的“ [Int:[String]]”类型下标! - Cannot subscript a value of type '[Int : [String]]' with an index of type 'String!' 无法用索引类型“ String”下标“ String”类型的值 - Cannot subscript a value of type 'String' with an index of type 'String' 不能下标类型为[String:AnyObject]的值?索引类型为String - Cannot subscript a value of type [String: AnyObject]? with an index of type String 迅速:无法下标[字典式]的值 <String, AnyObject> ],其索引类型为字符串 - swift: cannot subscript a value of type [Dictionary<String, AnyObject>] with an index of type string 无法用索引类型“ String”下标“ [[[String:Any]]””的值 - Cannot subscript a value of type '[[String : Any]]' with an index of type 'String' 无法使用索引类型为“String”的类型&#39;[String:AnyObject]&#39;下标值 - Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM