标签: swift4

如何在Swift 4可解码协议中解码具有JSON字典类型的属性

假设我的Customer数据类型包含一个metadata属性,该属性可以包含客户对象中的任何JSON字典

struct Customer {
  let id: String
  let email: String
  let metadata: [String: Any]
}
Run Code Online (Sandbox Code Playgroud)
{  
  "object": "customer",
  "id": "4yq6txdpfadhbaqnwp3",
  "email": "john.doe@example.com",
  "metadata": {
    "link_id": "linked-id",
    "buy_count": 4
  }
}
Run Code Online (Sandbox Code Playgroud)

metadata属性可以是任意JSON映射对象.

在我NSJSONDeserialization使用新的Swift 4 Decodable协议从反序列化的JSON中转换属性之前,我仍然无法想到这样做的方法.

有人知道如何使用可解码协议在Swift 4中实现这一目标吗?

json swift swift4 codable

84
推荐指数
6
解决办法
5万
查看次数

使用Swift 4.0编译的模块无法在Swift 4.0.1中导入

但是我使用相同的Xcode重新编译了框架,它仍然给我这个错误.

  • 适用于两者的基础SDK iOS 11.1
  • 两个版本的Swift语言版Swift 4.0
  • 不使用Pods/Carthage

我希望有人知道

xcode ios swift swift4 xcode9

83
推荐指数
5
解决办法
7万
查看次数

如何在Swift 4的可解码协议中使用自定义键?

Swift 4通过Decodable协议引入了对本机JSON编码和解码的支持.我如何使用自定义键?

比如说我有一个结构

struct Address:Codable {
    var street:String
    var zip:String
    var city:String
    var state:String
}
Run Code Online (Sandbox Code Playgroud)

我可以将其编码为JSON.

let address = Address(street: "Apple Bay Street", zip: "94608", city: "Emeryville", state: "California")

if let encoded = try? encoder.encode(address) {
    if let json = String(data: encoded, encoding: .utf8) {
        // Print JSON String
        print(json)

        // JSON string is 
           { "state":"California", 
             "street":"Apple Bay Street", 
             "zip":"94608", 
             "city":"Emeryville" 
           }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以将它编码回一个对象.

    let newAddress: Address = try decoder.decode(Address.self, from: encoded)
Run Code Online (Sandbox Code Playgroud)

但如果我有一个json对象

{ 
   "state":"California", 
   "street":"Apple Bay …
Run Code Online (Sandbox Code Playgroud)

json swift swift4 codable

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

Xcode 9:使用Swift 3.1编译的模块无法在Swift 4.0中导入

更新到Xcode 9后,我尝试构建我的一个项目.

我使用FacebookLogin pod.我在FacebookLogin/LoginButton.swift中有编译器错误

@testable import FacebookCore
? Module compiled with Swift 3.1 cannot be imported in Swift 4.0
Run Code Online (Sandbox Code Playgroud)

在我的目标构建设置中,Swift语言版本设置为Swift 3.2.

截图添加

我想我需要等待Facebook更新他们的pod?还是其他任何建议?

谢谢 !

ios facebook-login cocoapods swift4 xcode9-beta

78
推荐指数
7
解决办法
4万
查看次数

必须仅从主线程调用UIApplication.registerForRemoteNotifications()

Xcode 9(iOS 11)在注册Push(远程)通知时向我显示错误/警告.

这是错误消息

在此输入图像描述

这是代码,我尝试过:

let center  = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
        if error == nil{
              UIApplication.shared.registerForRemoteNotifications()
        }
 }
Run Code Online (Sandbox Code Playgroud)

错误/警告线:

UIApplication.shared.registerForRemoteNotifications()

怎么解决这个?

push-notification ios swift unusernotificationcenter swift4

71
推荐指数
5
解决办法
3万
查看次数

如何使用Swift Decodable协议解码嵌套的JSON结构?

这是我的JSON

{
    "id": 1,
    "user": {
        "user_name": "Tester",
        "real_info": {
            "full_name":"Jon Doe"
        }
    },
    "reviews_count": [
        {
            "count": 4
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这是我想要保存的结构(不完整)

struct ServerResponse: Decodable {
    var id: String
    var username: String
    var fullName: String
    var reviewCount: Int

    enum CodingKeys: String, CodingKey {
       case id, 
       // How do i get nested values?
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经查看了Apple的解码嵌套结构的文档,但我仍然不明白如何正确地执行不同级别的JSON.任何帮助都感激不尽.

json swift swift4 codable

71
推荐指数
5
解决办法
3万
查看次数

扩展中的声明无法覆盖Swift 4中的错误

我有一个扩展名:

public extension UIWindow {
    override public func topMostController()->UIViewController? { ... }
}
Run Code Online (Sandbox Code Playgroud)

但是对于我,topMostController我得到了下一个错误:

Declarations in extensions cannot override yet error
Run Code Online (Sandbox Code Playgroud)

它适用于Swift 3.1,但对于Swift 4,我得到了这个错误.怎么修好?他们在Swift 4中有什么变化?

ios swift swift-playground swift4

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

在Swift 4中使用Decodable继承

是否应该使用类继承来破坏类的可解码性.例如,以下代码

class Server : Codable {
    var id : Int?
}

class Development : Server {
    var name : String?
    var userId : Int?
}

var json = "{\"id\" : 1,\"name\" : \"Large Building Development\"}"
let jsonDecoder = JSONDecoder()
let item = try jsonDecoder.decode(Development.self, from:json.data(using: .utf8)!) as Development

print(item.id ?? "id is nil")
print(item.name ?? "name is nil") here
Run Code Online (Sandbox Code Playgroud)

输出是:

1
name is nil
Run Code Online (Sandbox Code Playgroud)

现在,如果我反转这个,名称解码但id没有.

class Server {
    var id : Int?
}

class Development : Server, Codable {
    var …
Run Code Online (Sandbox Code Playgroud)

swift swift4 codable

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

无法从Objective-C访问Swift 4类:"在类型对象上找不到属性"

使用最新的Xcode 9 beta,我似乎完全无法访问Swift类的属性.甚至更奇怪,我可以访问类本身来实例化它或其他什么,但完全无法访问它上面的属性.

所以,如果我有这个Swift类:

import UIKit

class TestViewController: UIViewController {
    var foobar = true
}
Run Code Online (Sandbox Code Playgroud)

我试着这样做:

TestViewController *testViewController = [[TestViewController alloc] init]; // success
testViewController.foobar; // error
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?Xcode 9的新项目.

xcode objective-c ios swift swift4

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

Swift 4可解码,直到解码时间才知道密钥

Swift 4可解码协议如何处理包含密钥的字典,该密钥的名称在运行时才知道?例如:

  [
    {
      "categoryName": "Trending",
      "Trending": [
        {
          "category": "Trending",
          "trailerPrice": "",
          "isFavourit": null,
          "isWatchlist": null
        }
      ]
    },
    {
      "categoryName": "Comedy",
      "Comedy": [
        {
          "category": "Comedy",
          "trailerPrice": "",
          "isFavourit": null,
          "isWatchlist": null
        }
      ]
    }
  ]
Run Code Online (Sandbox Code Playgroud)

这里我们有一系列字典; 所述第一有键categoryNameTrending,而第二个有密钥categoryNameComedy.categoryName键的值告诉我第二个键的名称.如何使用Decodable表达?

json swift4 decodable

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