假设我的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中实现这一目标吗?
但是我使用相同的Xcode重新编译了框架,它仍然给我这个错误.
我希望有人知道
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) 更新到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?还是其他任何建议?
谢谢 !
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()
怎么解决这个?
这是我的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)
我有一个扩展名:
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中有什么变化?
是否应该使用类继承来破坏类的可解码性.例如,以下代码
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) 使用最新的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的新项目.
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)
这里我们有一系列字典; 所述第一有键categoryName和Trending,而第二个有密钥categoryName和Comedy.categoryName键的值告诉我第二个键的名称.如何使用Decodable表达?