热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用iOS8的Swift3中的Alamofire-AlamofireinSwift3withiOS8

IveupdatedtothelatestversionofXcode(8)andafterupgradingthecodetoSwift3,Alamofires

I've updated to the latest version of Xcode (8) and after upgrading the code to Swift 3, Alamofire suddenly stops working and throwing a lot of errors.

我已经更新到最新版本的Xcode(8),在将代码升级到Swift 3之后,Alamofire突然停止工作并抛出了很多错误。

After a lot of research, I find that Alamofire for Swift 3 is compatible with iOS9+, so now I have to find another one that works with iOS8+.

经过大量的研究,我发现Alamofire for Swift 3与iOS9 +兼容,所以现在我必须找到另一个适用于iOS8 +的版本。

Do you know how can I make the same functionality as Alamofire for iOS8?

你知道我怎样才能为iOS8的Alamofire提供相同的功能?

2 个解决方案

#1


3  

try this fork, https://github.com/tonyli508/AlamofireDomain

试试这个分支,https://github.com/tonyli508/AlamofireDomain

or pod directly:

或直接:

pod 'AlamofireDomain', '~> 4.0'

pod'AlamofireDomain','〜> 4.0'

#2


2  

I didn't find a "real" alternative to Alamofire for iOS 8, so I tried using the URLRequest from Apple's Swift itself.

我没有为iOS 8找到Alamofire的“真正”替代品,所以我尝试使用Apple的Swift本身的URLRequest。

So here is the example I finally got after trying and trying (even without SwiftyJSON):

所以这是我在尝试和尝试后最终得到的例子(即使没有SwiftyJSON):

func searchPosts() -> [Post] {
    var request = URLRequest(url: URL(string: "https://www.mywebsite.com/searchPosts.php")!)
    request.httpMethod = "POST"
    let postString = ""
    request.httpBody = postString.data(using: .utf8)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = URLSession.shared.dataTask(with: request) { data, response, error in

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print(httpStatus.statusCode)
            self.showError()
        } else {
            do {
                self.listPosts = [Post]()
                // Convert NSData to Dictionary where keys are of type String, and values are of any type
                let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]

                for field in json["posts"] as? [AnyObject] ?? [] {

                    // Create "Post" object
                    let post = Post(
                        id: (field["id"])! as! String,
                        title: (field["title"] as! String),
                        alias: (field["alias"] as! String),
                        catid: (field["catid"] as! String),
                        catname: (field["catname"] as! String),
                        date: (field["date"] as! String),
                        image: (field["image"] as! String),
                        introtext: (field["introtext"] as! String),
                        fulltext: (field["fulltext"] as! String)
                    )
                    self.listPosts.append(post)
                }

                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            } catch {
                self.showError()
            }
        }
    }

    task.resume()

    return listPosts
}

I hope it could help other developers. I will be much appreciated if someone found another possible solution to this problem.

我希望它可以帮助其他开发人员。如果有人找到另一个可能解决这个问题的方法,我将不胜感激。

Regards


推荐阅读
author-avatar
凡秘能
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有