简体   繁体   English

SwiftyJson不起作用?

[英]SwiftyJson doesn't work?

在此处输入图片说明

I'm trying to get information off a website that I created myself, where I need to read a JSON object to import it's data into my iOS App (written in Swift). 我正在尝试从自己创建的网站上获取信息,我需要在该网站上读取JSON对象,以将其数据导入到我的iOS应用(用Swift编写)中。 I can only find very few tutorials on how to do that, and most of which are using a SWIFTYJson Library ( https://github.com/SwiftyJSON/SwiftyJSON ). 我只能找到很少的有关如何执行此操作的教程,并且大多数教程都使用SWIFTYJson库( https://github.com/SwiftyJSON/SwiftyJSON )。 The problem is when I use the Library the actual Library code itself seems to be having some errors, maybe due to the recent updates in Xcode. 问题是当我使用库时,实际的库代码本身似乎有一些错误,可能是由于Xcode的最新更新所致。 Even the example from the tutorial doesn't work. 即使是本教程中的示例也不起作用。

Has anyone different apporaches to solve this problem without using SwiftyJson? 有没有不使用SwiftyJson的不同解决方案来解决此问题?

Thanks 谢谢

This is the actual SwiftyJson file that i donwloaded. 这是我下载的实际SwiftyJson文件。 This is now in his own example where the code seems to be having some errors. 现在在他自己的示例中,代码似乎有一些错误。

As you stated in comment, you use Xcode 6.3 and this is the cause SwiftyJSON isn't working. 如评论中所述,您使用Xcode 6.3,这是SwiftyJSON无法正常工作的原因。 You've downloaded the newest version of the library, which is compatible with Swift 2.1 and Xcode 6.3 is bundled with Swift 1.2 if I recall correctly. 您已经下载了该库的最新版本,该库与Swift 2.1兼容,如果我没记错的话,Xcode 6.3与Swift 1.2捆绑在一起。 You should always use the newest version of the Xcode available (not counting beta releases). 您应该始终使用最新版本的Xcode(不包括Beta版本)。 Version of Xcode also affects version of Swift you have. Xcode的版本也会影响您拥有的Swift的版本。 As Swift is young language, it faces lots of changes between versions which triggers changes in frameworks written in Swift. 由于Swift是年轻的语言,因此它在版本之间面临许多变化,这会触发以Swift编写的框架的变化。

The first error which I can see "No type named Indexable..." seems to be reported in the SwiftyJSON project few months ago and the solution to it is to use Xcode 7 for building. 我看到的第一个错误似乎是几个月前在SwiftyJSON项目中报告的,该错误的解决方案是使用Xcode 7进行构建。

I can also see that you use iOS SDK 8.3, which probably could be updated to 9.2 and fix the problem. 我还可以看到您使用的是iOS SDK 8.3,它可能会更新到9.2并解决了问题。 If you use Xcode 6, try updating to Xcode 7 and the errors will probably be fixed. 如果您使用Xcode 6,请尝试更新到Xcode 7,错误可能会得到解决。 Also I don't see any reason to use old SDK version if a new one is available. 另外,如果有新版本可用,我看不出有任何理由使用旧版SDK。

Try import SwiftyJSON file as below without project. 尝试按以下方式导入SwiftyJSON文件而不使用项目。 在此处输入图片说明

If you want to use like your version: 如果要使用与您的版本类似的代码:

Try to create a new swift file and import this swiftyJSON version manually. 尝试创建一个新的swift文件并手动导入此swiftyJSON version (not the latest) This should work with xcode 7.2 and Swift 2 and deployment target 8.0. (不是最新的)这应该与xcode 7.2Swift 2deployment target 8.0.

https://gist.github.com/fatihyildizhan/450822246e8deb14099c https://gist.github.com/fatihyildizhan/450822246e8deb14099c

Requirements iOS 7.0+ / Mac OS X 10.9+, Xcode 7 要求iOS 7.0+ / Mac OS X 10.9 +,Xcode 7

Don't forget to import Alamofire and SwiftyJSON . 不要忘记导入AlamofireSwiftyJSON

 // This is Alamofire Request
  func LoginWithAPI(completion : (String) -> ()) {
    Alamofire.request(.POST, "your_api_url", parameters: ["username":"bodrum", "password":"yalikavak"], encoding: ParameterEncoding.JSON)
      .validate()
      .responseJSON { response in
        switch response.result {
        case .Success:
          // Parse your result to JSON object first.
          // JSON is SwiftyJSON object.
          let jsonResponse = JSON(data: response.data!)
          let result = jsonResponse["result"].stringValue
          completion(result)
        case .Failure(let error):
          completion(error.description)
        }
    }
  }

if you want to add any header , use this line: 如果要添加任何header ,请使用以下行:

Alamofire.request(.POST, "your_api_url", parameters: ["username":"bodrum", "password":"yalikavak"], encoding: ParameterEncoding.JSON,
  headers: ["header_key": "header_value"])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM