Questions tagged [swift-optionals]

56 questions
9
votes
1 answer

Protocol conformance with implicitly unwrapped optionals

I am trying to make a Swift protocol that I can use on UILabel, UITextField, and UITextView that incorporate their text, attributedText, and font properties. However, unfortunately these three classes are inconsistent with whether they use optional…
shim
  • 9,289
  • 12
  • 69
  • 108
4
votes
1 answer

How to use published optional properties correctly for SwiftUI

To provide some context, Im writing an order tracking section of our app, which reloads the order status from the server every so-often. The UI on-screen is developed in SwiftUI. I require an optional image on screen that changes as the order…
Chris G
  • 81
  • 1
  • 7
4
votes
1 answer

Default value for optional type 'URL?'

I have an extension allowing me to download images to my UITableView from API. extension UIImageView { func donwloadImage(from url: String) { let urlRequest = URLRequest(url: URL(string: url)!) let task =…
Russ
  • 99
  • 1
  • 7
2
votes
2 answers

What is the best method in preventing this Optional-related crash?

Context I am beginner learning Swift and I'm trying to make sure I understand this bug "fix". I followed along a Youtube video showing how to create a timer in Xcode. I changed some things around in effort to learn some things but the issue that…
2
votes
2 answers

I expected the system to report non protocol conformance, but it does not! Why?

I am using Xcode Version 11.3.1 (11C504) I am trying to create a generic function in Swift that will reject its parameter unless such a parameter is Optional. In the following code, I was expecting the system to report errors in all calls to…
SirEnder
  • 564
  • 4
  • 14
2
votes
3 answers

Any? incorect semantics

I was playing around with some code in swift and encountered one interesting case. Lets start with a little preamble: suppose you create some optional variables: let a: String? = "abcd"; let b: Int? = 4 print( "Type of \(a) is \(type(of: a))"…
oneWayTicket
  • 101
  • 6
1
vote
1 answer

Getting error with previously supported Optional type requirements on a function declaration (same-type, error in Swift 6)

This currently works, but in xcode-beta it is now showing a warning: Same-type requirement makes generic parameter 'Value' non-generic; this is an error in Swift 6 func getUDValue(_ key: String, _ defaultValue: Value) -> Value where Value…
Doghound
  • 129
  • 1
  • 4
1
vote
1 answer

Split String with varied amount of input data

I have a string that looks like this: https://product/000000/product-name-type-color I used a split to separate this strings, but Im having problems because the link can come without the description or the id guard let separateLink =…
1
vote
3 answers

How can I compare two different types of data (String and Int) using readLine() on Swift?

everyone! I'm a new member of Stack Overflow, just like I'm beginner on swift programming. I'm making this post to find out a solution for the following case: I'm creating on Swift an app using the Command Line Tool for inputing data. The app…
1
vote
1 answer

What is the purpose of a line of code that says: '_ = viewController.view'?

What is the purpose of the following line of code? It doesn't seem to do anything. If it were in Playground or in a print statement it would at least show something. _ = masterVC.view It's in Apple's sample code at Sharing CloudKit Data with Other…
daniel
  • 1,446
  • 3
  • 29
  • 65
1
vote
1 answer

Swift - Sort array by optionals

So if have an array of ToDos and the Todo objects have the properties due(Bool) which has the info if the ToDo has a due date; dueDate(Date) which is an optional so if due is true dueDate != nil, if due is false dueDate = nil; creationDate(Date):…
Timothy
  • 27
  • 4
1
vote
1 answer

How do I access an outlet from another View Controller? (Swift)

I am new to Swift programming and am trying to make a simple app that presents a Challenge which you can then complete by pressing a button. Then in a separate view controller you unlock images when you have completed a certain number of…
1
vote
1 answer

Swift safe unwrapping optional a Class Constant without redundancy

I am trying to get more Structure into my code so I am implementing this UserDefaultsService: class UserDefaultsService { let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) static let shared = UserDefaultsService() …
Chris
  • 1,828
  • 6
  • 40
  • 108
1
vote
2 answers

How do I unwrap a printed returned optional in a function (exercise)

Hi I'm trying to learn Swift using Apples Fundamentals of Swift book. There has been a similar post in the past but I have some problems with the answer provided that I would like clarified. The post was Functions and optionals exercise I'm on this…
JGW
  • 43
  • 1
  • 6
1
vote
1 answer

Getting a value of "Optional()" even when unwrapping

I'm trying to initialize parameters for an Alamofire request. For some reason, the value for one of the parameters always ends up with a value of "Optional()", even when I'm unwrapping it. Here is what I've tried... var params : [String : Any] =…
Lastmboy
  • 1,849
  • 3
  • 21
  • 39
1
2 3 4