标签: swift4

代码从Swift 2.x迁移到Swift 4

我们有足够大的项目,使用Swift 2.x构建,现在Apple刚刚发布了Swift 4,以便向前推进最新版本的Swift,我们可以选择这条路径......

我们需要先将代码迁移到Swift 3吗?或者我们可以使用Xcode 9直接将代码迁移到Swift 4?

swift swift2 xcode8 swift4 xcode9-beta

28
推荐指数
3
解决办法
2万
查看次数

无法加载优化模型 - GoogleMaps SDK IOS

从CocoaPods安装Google Maps SDK后,我收到此错误.

CoreData: annotation:  Failed to load optimized model at path '/Users/nabeel/Library/Developer/CoreSimulator/Devices/96078737-8063-4BC1-97DB-7FECEC6835D9/data/Containers/Bundle/Application/972CD686-82DD-4357-9CDD-65A735D82190/My-APP-Beta.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileVersionID.omo'
CoreData: annotation:  Failed to load optimized model at path '/Users/nabeel/Library/Developer/CoreSimulator/Devices/96078737-8063-4BC1-97DB-7FECEC6835D9/data/Containers/Bundle/Application/972CD686-82DD-4357-9CDD-65A735D82190/My-APP-Beta.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileVersionID.omo'
CoreData: annotation:  Failed to load optimized model at path '/Users/nabeel/Library/Developer/CoreSimulator/Devices/96078737-8063-4BC1-97DB-7FECEC6835D9/data/Containers/Bundle/Application/972CD686-82DD-4357-9CDD-65A735D82190/My-APP-Beta.app/GoogleMaps.bundle/GMSCacheStorage.momd/StorageWithTileVersionID.omo'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了pod更新和pod安装,但同样的问题.

sdk google-maps ios swift4 xcode9.1

28
推荐指数
3
解决办法
1万
查看次数

无法使用Swift 4 Package Manager导入包

尝试在我的机器上使用Xcode-beta(v9)测试Swift 4,并将包导入测试项目时出现问题:

  • 启动项目使用 swift package init --type executable
  • 更改Package.swift并添加了2个项目以试用:

Package.swift

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "sampleproject",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/IBM-Swift/Kitura.git", from: "1.7.6"),
        .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.5.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can …
Run Code Online (Sandbox Code Playgroud)

macos xcode package-managers swift swift4

27
推荐指数
1
解决办法
7515
查看次数

非标称类型X不支持显式初始化

我试图理解我在swift中对泛型做错了什么.

我创建了这个示例游乐场

import UIKit

public protocol MainControllerToModelInterface : class {
    func addGoal()
    init()
}

public protocol MainViewControllerInterface : class {
    associatedtype MODELVIEW
    var modelView: MODELVIEW? {get set}

    init(modelView: MODELVIEW)
}

public class MainViewController<M> : UIViewController, MainViewControllerInterface where M : MainControllerToModelInterface {
    public weak var modelView: M?

    required public init(modelView: M) {
        self.modelView = modelView
        super.init(nibName: String(describing: MainViewController.self), bundle: Bundle.main)
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

public class Other<C, M> : NSObject where …
Run Code Online (Sandbox Code Playgroud)

generics xcode ios swift swift4

26
推荐指数
1
解决办法
1万
查看次数

在Swift 4中,如何删除基于块的KVO观察者?

如果我存储这样的观察者:

let observer: NSKeyValueObservation = foo.observe(\.value, options: [.new]) { (foo, change) in
    print(change.newValue)
}
Run Code Online (Sandbox Code Playgroud)

observer一旦我不再需要它,如何删除/禁用/清除?

我的foo实例没有remove接收NSKeyValueObservation实例的任何类似方法,它observer本身也没有任何remove类似的实例.

key-value-observing swift swift4

26
推荐指数
1
解决办法
8434
查看次数

使用带有密钥的可编码,有时是Int,有时是String

我有一个API,有时会id在JSON中作为Int 返回一个特定的键(在本例中),有时它将返回与String相同的键.如何使用codable来解析JSON?

struct GeneralProduct: Codable {
    var price:Double!
    var id:String?
    var name:String!

    private enum CodingKeys: String, CodingKey {
        case price = "p"
        case id = "i"
        case name = "n"
    }

    init(price:Double? = nil, id: String? = nil, name: String? = nil) {
        self.price = price
        self.id = id
        self.name = name
    }
}
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误消息:Expected to decode String but found a number instead.返回数字的原因是因为id字段为空,并且当id字段为空时,它默认返回0作为可编码标识为数字的ID.我基本上可以忽略ID键但是可编码不会让我根据我的知识忽略它.处理这个问题的最佳方法是什么?

这是JSON.它非常简单

工作

{
  "p":2.12,
  "i":"3k3mkfnk3",
  "n":"Blue Shirt"
}
Run Code Online (Sandbox Code Playgroud)

错误 - 因为系统中没有id,它返回0作为默认值,可编码显然看作是与字符串相对的数字.

{
  "p":2.19, …
Run Code Online (Sandbox Code Playgroud)

json swift swift4 codable

26
推荐指数
4
解决办法
9040
查看次数

Swift 4"这个类不符合键值编码"

我有一个雨燕库,它是严重依赖obj.valueForKey()NSObject.

迁移到Swift 4后,我发现这些调用总是崩溃,错误"这个类不是密钥值的密钥值...",除非我正在寻找的属性被声明@objc.

现在是否必须声明@objc使用此方法找到它们的属性?还有其他选择吗?

xcode ios swift swift4

25
推荐指数
1
解决办法
8903
查看次数

同时访问0x1c0a7f0f8,但修改需要Xcode 9 beta 4上的独占访问错误

我的项目使用Objective-C和Swift代码.当用户登录时,它会为用户首选项调用一组apis,我有一个DataCoordinator.swift类来调度API操作,我从UserDetailViewController.m类调用此类来加载用户首选项.在使用Xcode 9 beta 4将我的代码迁移到Swift 4之前,这种用法正常工作.现在当我登录时,我在DataCoordinator类中给出了这个错误.下面是我的DataCoordinator和Viewcontroller类的示例.

DataCoordinator.swift

import UIKit

@objcMembers

class DataCoordinator: NSObject {

    //MARK:- Private
    fileprivate var user = myDataStore.sharedInstance().user
    fileprivate var preferenceFetchOperations = [FetchOperation]()

    fileprivate func scheduleFetchOperation(_ operation:FetchOperation, inFetchOperations operations:inout [FetchOperation]) {
        guard  operations.index(of: operation) == nil else { return }
        operations.append(operation)
    }

    fileprivate func completeFetchOperation(_ fetchOperation:FetchOperation, withError error:Error?, andCompletionHandler handler:@escaping FetchCompletionHandler) {

        func removeOperation(_ operation:FetchOperation, fromOperations operations:inout [FetchOperation]) {
            if operations.count > 0 {
                operations.remove(at: operations.index(of: fetchOperation)!)                 
              handler(error)
            }
        }

        if preferenceFetchOperations.contains(fetchOperation) {
            removeOperation(fetchOperation, fromOperations: &preferenceFetchOperations)
        }

    } …
Run Code Online (Sandbox Code Playgroud)

crash swift swift4 ios11 xcode9-beta

25
推荐指数
6
解决办法
2万
查看次数

iOS:在面向iOS 9时使用Swift 4

Swift 4正在添加一些非常酷的功能,如强类型键路径和JSON编码/解码使用Codable.

我想在仍然针对iOS 9及更高版本的同时使用这些新功能.

据我所知,Swift语言版本并未绑定到iOS版本,例如与Foundation框架相反.但是,如果这是正确的,我找不到任何信息.

在针对11岁以上的iOS时,我可以使用Swift 4吗?

ios swift swift4

24
推荐指数
1
解决办法
8730
查看次数

Swift 4 JSON可解码最简单的解码类型更改方法

使用swift4的Codable协议,有很多级别的引擎日期和数据转换策略.

鉴于JSON:

{
    "name": "Bob",
    "age": 25,
    "tax_rate": "4.25"
}
Run Code Online (Sandbox Code Playgroud)

我想将它强制进入以下结构

struct ExampleJson: Decodable {
    var name: String
    var age: Int
    var taxRate: Float

    enum CodingKeys: String, CodingKey {
       case name, age 
       case taxRate = "tax_rate"
    }
}
Run Code Online (Sandbox Code Playgroud)

日期解码策略可以将基于字符串的日期转换为日期.

基于String的Float是否有某种功能

否则我一直坚持使用CodingKey引入一个String并使用计算得到:

    enum CodingKeys: String, CodingKey {
       case name, age 
       case sTaxRate = "tax_rate"
    }
    var sTaxRate: String
    var taxRate: Float { return Float(sTaxRate) ?? 0.0 }
Run Code Online (Sandbox Code Playgroud)

这种方式比我看起来需要更多的维护.

这是最简单的方式还是类似于DateDecodingStrategy用于其他类型的转换?

更新:我应该注意:我也走了超越的路线

init(from decoder:Decoder)
Run Code Online (Sandbox Code Playgroud)

但这是相反的方向,因为它迫使我为自己做这一切.

json swift swift4 codable

24
推荐指数
3
解决办法
1万
查看次数