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
48
votes
3 answers

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

I am using Codable protocol from Swift 4 first time, I am not able to understand use of decodeIfPresent from Decodable. /// Decodes a value of the given type for the given key, if present. /// /// This method returns `nil` if the container does not…
technerd
  • 14,144
  • 10
  • 61
  • 92
48
votes
8 answers

Single line if statement in Swift

How would one convert the following to Swift from Objective-C? if (myVar) return; Swift does not use parentheses around the conditional, however the following code gives an error. if myVar return
BytesGuy
  • 4,097
  • 6
  • 36
  • 55
47
votes
3 answers

UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:)

I've tried to upload a photo to Firebase but it's giving me this error. It was working before Xcode 10. I'm getting this error: 'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)' and I don't…
b3f79
  • 475
  • 1
  • 4
  • 4
47
votes
2 answers

Swift 4 approach for observeValue(forKeyPath:...)

I've been trying to find an example, but what I've seen doesn't work in my case. What would be the equivalent of the following code: object.addObserver(self, forKeyPath: "keyPath", options: [.new], context: nil) override public func observeValue( …
MVZ
  • 2,220
  • 5
  • 27
  • 57
45
votes
2 answers

In Swift, can you split a string by another string, not just a character?

In Swift, it's easy to split a string on a character and return the result in an array. What I'm wondering is if you can split a string by another string instead of just a single character, like so... let inputString = "This123Is123A123Test" let…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
45
votes
5 answers

iOS view visibility gone

I am new to iOS development.I want to toggle (hide/visible) a subview from parent view.In android there is a way to hide the visibility to gone. In android subView.setVisibility(View.GONE); In iOS subView.removeFromSuperview() when i use above…
John
  • 3,769
  • 4
  • 12
  • 23
45
votes
4 answers

Swift 4 Codable; How to decode object with single root-level key

I'm using the Swift 4 Codable protocol with JSON data. My data is formatted such that there is a single key at the root level with an object value containing the properties I need, such as: { "user": { "id": 1, "username": "jdoe" } } I…
Joshua Breeden
  • 1,844
  • 1
  • 16
  • 29
44
votes
4 answers

Barcode on swift 4

I'm trying to upgrade mi app to swift 4, but the barcode reader is not working. I have isolated the barcode reader code, and still not working. The camera works but it does not detect the barcode. The code worked just fine on swift 3 iOS 10. This is…
Dx_
  • 1,566
  • 1
  • 16
  • 17
43
votes
5 answers

How to build Swift 3 project on Xcode 9?

Xcode 9 Release Notes says that Swift 3 compilation is supported: One compiler for Swift 4 and Swift 3, Swift 4 and Swift 3 targets can be compiled together in the same project. I have installed Xcode 9 beta. But when I press Build it shows bunch…
Exey Panteleev
  • 1,260
  • 3
  • 13
  • 15
42
votes
2 answers

In Swift 4, how do I remove a block-based KVO observer?

If I store an observer like this: let observer: NSKeyValueObservation = foo.observe(\.value, options: [.new]) { (foo, change) in print(change.newValue) } How do I remove/disable/cleanup observer once I no longer need it? My foo instance does…
Guilherme
  • 7,839
  • 9
  • 56
  • 99
41
votes
8 answers

How to conform UIImage to Codable?

Swift 4 has Codable and it's awesome. But UIImage does not conform to it by default. How can we do that? I tried with singleValueContainer and unkeyedContainer extension UIImage: Codable { // 'required' initializer must be declared directly in…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
40
votes
3 answers

How to fix "Read-Write-Data Sandbox: error when using Mac Catalyst

I recently updated to macOS Catalina so I could update some of my apps with Mac support using Catalyst. Whenever I run the app and it tries to access the CloudKit data (I use CloudKit to sync Core Data, Data if an iCloud Account is available), it…
117MasterChief96
  • 548
  • 1
  • 5
  • 16
39
votes
8 answers

Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentReadingOptionKey

I found this string extension somewhere on SO that allows me to turn html code into an attributed string: func html2AttributedString() -> NSAttributedString { return try! NSAttributedString(data: self.data(using: String.Encoding.unicode,…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
38
votes
5 answers

type A requires that type B be a class type swift 4

the following code gives me a compile error 'WeakReference' requires that 'ServiceDelegate' be a class type protocol ServiceDelegate: AnyObject { func doIt() } class SomeClass() { // compile error occurs in this line private var…
christopher.online
  • 2,614
  • 3
  • 28
  • 52
36
votes
2 answers

iso8601 date json decoding using swift4

So, I've got iso8601 dates in my json which look like "2016-06-07T17:20:00.000+02:00" Is there a way to parse these iso8601 dates using swift4? Am I missing something obvious? I tried the following, but only the dateString "2016-06-07T17:20:00Z"…
Tycho Pandelaar
  • 7,367
  • 8
  • 44
  • 70