Questions tagged [swift4]

Use this tag only for questions directly related to changes in version 4 of Apple's Swift programming language. Use the tag [swift] for more general language questions, or the tags [ios], [cocoa], [apple-watch] etc. for questions about developing on Apple platforms.

Swift 4 is the fourth version of the Swift language developed by Apple that was released in 2017. The language is open source and available on Github (see ).

The development of Swift 4 and its goals can be viewed on the Swift Programming Language Evolution repository on GitHub.

5684 questions
495
votes
12 answers

The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

Briefly, while using Xcode 9 Beta, I have run into the following warning: The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc…
DaleK
  • 5,009
  • 3
  • 10
  • 8
480
votes
9 answers

How do I write dispatch_after GCD in Swift 3, 4, and 5?

In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime,…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
362
votes
21 answers

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

I have the following simple code written in Swift 3: let str = "Hello, playground" let index = str.index(of: ",")! let newStr = str.substring(to: index) From Xcode 9 beta 5, I get the following warning: 'substring(to:)' is deprecated: Please use…
Adrian
  • 19,440
  • 34
  • 112
  • 219
198
votes
23 answers

Command CompileSwift failed with a nonzero exit code in Xcode 10

After updating to the latest version of Xcode at the moment (version 10.0) the project is unable to build because it found some errors regarding some "Command CompileSwift failed with a nonzero exit code" error. How do I solve this errors? They…
Jordi Gámez
  • 3,400
  • 3
  • 22
  • 35
176
votes
8 answers

warning: 'characters' is deprecated: Please use String or Substring directly

characters - an instance property of String, is deprecated from with Xcode 9.1 It was very useful to get a substring from String by using the characters property but now it has been deprecated and Xcode suggests to use substring. I've tried to check…
Krunal
  • 77,632
  • 48
  • 245
  • 261
175
votes
8 answers

With JSONDecoder in Swift 4, can missing keys use a default value instead of having to be optional properties?

Swift 4 added the new Codable protocol. When I use JSONDecoder it seems to require all the non-optional properties of my Codable class to have keys in the JSON or it throws an error. Making every property of my class optional seems like an…
zekel
  • 9,227
  • 10
  • 65
  • 96
171
votes
16 answers

How to decode a property with type of JSON dictionary in Swift [45] decodable protocol

Let's say I have Customer data type which contains a metadata property that can contains any JSON dictionary in the customer object struct Customer { let id: String let email: String let metadata: [String: Any] } { "object": "customer", …
167
votes
16 answers

How can I use Swift’s Codable to encode into a dictionary?

I have a struct that implements Swift 4’s Codable. Is there a simple built-in way to encode that struct into a dictionary? let struct = Foo(a: 1, b: 2) let dict = something(struct) // now dict is ["a": 1, "b": 2]
zoul
  • 102,279
  • 44
  • 260
  • 354
166
votes
16 answers

Swift JSONDecode decoding arrays fails if single element decoding fails

While using Swift4 and Codable protocols I got the following problem - it looks like there is no way to allow JSONDecoder to skip elements in an array. For example, I have the following JSON: [ { "name": "Banana", "points": 200, …
Khriapin Dmitriy
  • 1,762
  • 2
  • 10
  • 9
144
votes
4 answers

How do I use custom keys with Swift 4's Decodable protocol?

Swift 4 introduced support for native JSON encoding and decoding via the Decodable protocol. How do I use custom keys for this? E.g., say I have a struct struct Address:Codable { var street:String var zip:String var city:String var…
chrismanderson
  • 4,603
  • 5
  • 31
  • 47
135
votes
5 answers

How can I deal with @objc inference deprecation with #selector() in Swift 4?

I'm trying to convert my project's source code from Swift 3 to Swift 4. One warning Xcode is giving me is about my selectors. For instance, I add a target to a button using a regular selector like this: button.addTarget(self, action:…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
129
votes
7 answers

How to decode a nested JSON struct with Swift Decodable protocol?

Here is my JSON { "id": 1, "user": { "user_name": "Tester", "real_info": { "full_name":"Jon Doe" } }, "reviews_count": [ { "count": 4 } ] } Here is the structure I…
Just a coder
  • 15,480
  • 16
  • 85
  • 138
106
votes
12 answers

Codable class does not conform to protocol Decodable

Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { …
Melodius
  • 2,505
  • 3
  • 22
  • 37
101
votes
7 answers

Using Decodable in Swift 4 with Inheritance

Should the use of class inheritance break the Decodability of class. For example, the following code class Server : Codable { var id : Int? } class Development : Server { var name : String? var userId : Int? } var json = "{\"id\" :…
Kevin McQuown
  • 1,277
  • 2
  • 9
  • 13
101
votes
12 answers

navigation bar rightbaritem image-button bug iOS 11

This code works ok in ios10. i get my label and an image button which is the user photo profile, circular round.. ok. but when running xcode 9 ios11 simulator i get it streched out. the button frame has to be 32x32 , when checking on the sim and…
lorenzo gonzalez
  • 1,894
  • 4
  • 14
  • 18
1
2 3
99 100