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
92
votes
6 answers

Encode nil value as null with JSONEncoder

I'm using Swift 4's JSONEncoder. I have a Codable struct with an optional property, and I'd like this property to show up as null value in the produced JSON data when the value is nil. However, JSONEncoder discards the property and does not add it…
dr_barto
  • 5,723
  • 3
  • 26
  • 47
92
votes
9 answers

Module compiled with Swift 4.0 cannot be imported in Swift 4.0.1

However I have recompiled the framework using the same Xcode and it still gives me this error. Base SDK iOS 11.1 for both Swift Language Version Swift 4.0 for both Not using Pods/Carthage I hope someone might know
Simon
  • 2,419
  • 2
  • 18
  • 30
86
votes
8 answers

UIApplication.registerForRemoteNotifications() must be called from main thread only

Xcode 9 (iOS 11) showing me an error/warning while registering for Push (remote) notification. Here is error message And here is code, I've tried: let center = UNUserNotificationCenter.current() center.delegate =…
Krunal
  • 77,632
  • 48
  • 245
  • 261
80
votes
14 answers

Xcode 9 : Module compiled with Swift 3.1 cannot be imported in Swift 4.0

After updating to Xcode 9, I tried to build one of my projects. I use the FacebookLogin pod. I have a compiler error in FacebookLogin/LoginButton.swift @testable import FacebookCore ❌ Module compiled with Swift 3.1 cannot be imported in Swift…
Arnaud
  • 955
  • 1
  • 6
  • 15
78
votes
9 answers

'subscript' is unavailable: cannot subscript String with a CountableClosedRange, see the documentation comment for discussion

In Swift 4, I'm getting this error when I try to take a Substring of a String using subscript syntax. 'subscript' is unavailable: cannot subscript String with a CountableClosedRange, see the documentation comment for discussion For example: let…
Barry Jones
  • 1,329
  • 1
  • 9
  • 16
75
votes
6 answers

Unable to access Swift 4 class from Objective-C: "Property not found on object of type"

Using the latest Xcode 9 beta, I'm seemingly completely unable to access properties on Swift classes. Even odder, I can access the class itself to instantiate it or whatever, but completely unable to access properties on it. So if I have this Swift…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
71
votes
10 answers

Codable enum with default case in Swift 4

I have defined an enum as follows: enum Type: String, Codable { case text = "text" case image = "image" case document = "document" case profile = "profile" case sign = "sign" case inputDate = "input_date" case inputText =…
LucaRoverelli
  • 1,116
  • 1
  • 9
  • 15
69
votes
5 answers

Using codable with value that is sometimes an Int and other times a String

I have an API that will sometimes return a specific key value (in this case id) in the JSON as an Int and other times it will return that same key value as a String. How do I use codable to parse that JSON? struct GeneralProduct: Codable { var…
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58
65
votes
1 answer

Custom Swift Encoder/Decoder for the Strings Resource Format

I have been playing around with Codable and reading and writing JSON from and to a file. Now I would like to write a custom Coder that can read and write iOS .strings files. Can anyone help me with this? I found the protocols Encoder and Decoder,…
Banana
  • 4,010
  • 9
  • 33
  • 49
64
votes
3 answers

Declarations in extensions cannot override yet error in Swift 4

I have an extension: public extension UIWindow { override public func topMostController()->UIViewController? { ... } } but for my topMostController I get the next error: Declarations in extensions cannot override yet error It works well for…
J. Doe
  • 683
  • 1
  • 5
  • 7
62
votes
5 answers

Encode/Decode Array of Types conforming to protocol with JSONEncoder

I'm trying to find the best way to Encode/Decode an array of structs conforming to a swift protocol using the new JSONDecoder/Encoder in Swift 4. I made up a little example to illustrate the problem: First we have a protocol Tag and some Types that…
goesta
  • 649
  • 1
  • 6
  • 13
58
votes
4 answers

How to use swift 4 Codable in Core Data?

Codable seems a very exciting feature. But I wonder how we can use it in Core Data? In particular, is it possible to directly encode/decode a JSON from/to a NSManagedObject? I tried a very simple example: and defined Foo myself: import…
hgl
  • 2,034
  • 4
  • 21
  • 30
55
votes
6 answers

Getting string from Swift 4 new key path syntax?

How can you get a string value from Swift 4 smart keypaths syntax (e.g., \Foo.bar)? At this point I'm curious about any way at all, does not matter if it's complicated. I like the idea of type information being associated with smart key path. But…
Dannie P
  • 4,464
  • 3
  • 29
  • 48
55
votes
15 answers

Simultaneous accesses to 0x1c0a7f0f8, but modification requires exclusive access error on Xcode 9 beta 4

my project uses both Objective-C and Swift code. When a user logs in, it calls a set of apis for user preference, I have a DataCoordinator.swift class which schedules the API operation and I make this calls from UserDetailViewController.m class to…
Francis F
  • 3,157
  • 3
  • 41
  • 79
50
votes
5 answers

Generic Swift 4 enum with Void associated type

tl;dr Is it possible to instantiate a generic Swift 4 enum member with an associated value of type Void? Background I'm using a simple Result enum (similar to antitypical Result): enum Result { case success(T) case error(Error?) } Now I'd…
dr_barto
  • 5,723
  • 3
  • 26
  • 47