Questions tagged [swift-optionals]

56 questions
0
votes
1 answer

Force unwrap after guard let

I was told my code contains a lot of force unwrapping. I thought it's okay to do that if I am sure that the value operated won't be nil: private var x: Int? private var y: Int? @IBAction func startButtonPressed(_ sender: UIButton) { guard…
0
votes
2 answers

Swift Extension - Optional

I'm trying to extend optionals into something readable, and achieved this so far: @discardableResult func isNotNil(_ handler: (Wrapped) -> Void) -> Optional { switch self { case .some(let value): handler(value) …
bhagat
  • 38
  • 4
0
votes
2 answers

CompactMapValues not working on this simple dictionary

I have a model that looks like: public struct Profile { public let bio: String? public let company: String public let createdDate: Date public let department: String public let email: String public let firstName: String public let…
Harry Blue
  • 4,202
  • 10
  • 39
  • 78
0
votes
2 answers

Cannot assign value of type 'String??' to type 'String?' error

let say I defined a class class Dummy { var title: String? } and I have a dictionary as let foo: [Int: String?] = [:] then when I make an assignment as below var dummy = Dummy() dummy.title = foo[1] it says Cannot assign value of type…
plymr
  • 45
  • 7
0
votes
1 answer

Optional Still Returning Nil After Assigning Value

I am working on a similar feature to 'liking/unliking a post'. I have an MVVM architecture as; struct MyStructModel { var isLiked: Bool? = false } class MyStructView { var isLiked: Bool init(myStructModel: MyStructModel) { …
David Henry
  • 1,972
  • 20
  • 43
0
votes
1 answer

Unwrapping issue with AXUIElementCopyAttributeValue in Mac OS

I'm trying to copy some information regarding an accessibility window option. Unfortunately, I can't resolve an error that's caused by the AXUIElementCopyAttributeValue method, despite passing in what appears to be all the correct types as…
camelCaseCowboy
  • 946
  • 1
  • 10
  • 26
0
votes
1 answer

Duplicating a game scene for a secondary level?

I’m currently working on a sprite kit - I have a first level complete. My game scene calls from encounterManager for the scenes I have prepared. I am curious as if it is possible to duplicate the game scene in order to set-up a secondary level? I…
AJ James
  • 11
  • 5
0
votes
2 answers

Swif struct not conform to Codable

I'm trying to let a struct with optionals being codable/decodable but I receive an error message: Type 'item' does not conform to protocol 'Encodable' here is the code: struct Item: Codable { let domanda: String let rispostaSemplice:…
Manuel Zompetta
  • 394
  • 4
  • 17
0
votes
2 answers

Loop through plist in swift

Hey guys I'm currently trying to loop through a plist that is set up like this: The code I'm using: letterPoints = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "LetterPoints", ofType: "plist")!) for (myKey, myValue) in…
Slacky88
  • 67
  • 4
0
votes
0 answers

Getting fatal error when AVAudioPlayer sound triggered by timer function in swift

Trying to trigger a sound file with timer function, but after timer count reaching to trigger the audio file, I get this error: Fatal error: Unexpectedly found nil while unwrapping an Optional value. I can't find the problem related to the…
Bezi
  • 57
  • 1
  • 7
0
votes
1 answer

Swift optionals in class methods

I am still relatively new to swift, so I am having some problems with the proper syntax. Here is my code for the class Date, which has the isLeapYear and daysInMonth methods. I am having problems with the optionals for these methods: class Date { …
EvilTaco
  • 133
  • 2
  • 8
0
votes
1 answer

How do you create an optional variable that is assigned a function in Swift

I would like to create an optional variable that can be assigned a function ( or can be nil). I'm guessing it would look something like this but the following code does not compile. var variableThatWillBeAssignedAFunction: {(Int) -> Int}? Thanks…
code kid
  • 374
  • 1
  • 6
  • 22
0
votes
1 answer

Kotlin Multiplatform keep optionals iOS framework

I'm currently building an API library with Kotlin Multiplatform, which I want to share between Android & iOS projects. API responses, in some cases, contain optionals (which is a great feature of both Kotlin & Swift). For example: data class…
Daan
  • 475
  • 5
  • 17
0
votes
2 answers

Problem: My non optional variable is nil. (Fatal Error while unwrapping)

I am trying to update an UILabel. @IBOutlet weak var balanceLabel: UILabel! it is initially declared as a non optional variable: var balanceValue: Int = 1 The UILabel depends on the not optional variable balanceValue. func updateBalanceLabel()…
Roman P.
  • 13
  • 6
0
votes
1 answer

Swift ignoring parameter types defined in block signature

I have the code below, which the compiler is happy with: func CheckPaintExists(colorCode : String, applicationCode : String) { let checkRequest = NSFetchRequest(entityName: "Paint") checkRequest.predicate =…
pnizzle
  • 6,243
  • 4
  • 52
  • 81