Questions tagged [optional-variables]

36 questions
1
vote
2 answers

Swift expression, empty or nonexistent dictionary in array - reliably zero?

A String dictionary of arrays blah: [String:[Stuff]] For a given key, say "foo", I want to know how many items are in that array - but, if there is no such array, I just want to get zero. I'm doing this ... blah["foo"]?.count ?? 0 so if (…
Fattie
  • 27,874
  • 70
  • 431
  • 719
1
vote
5 answers

little confuse about optionals in swift

Maybe I'm very confused idk yet. I thought i understood the basic concept but when playing around in xcode I'm not getting what I thought I should be Take the following code var title: String? = "hello world" println("\(title)") I thought that this…
user3275730
  • 25
  • 1
  • 6
1
vote
1 answer

Swift optional variable not set using Xcode 6.1

I'm trying to build my first Swift application. In this application I'm looping over an KML file that contains information about some restaurant and for each one of them I'm trying to build a Place object with the available information, compare a…
maxwell2022
  • 2,818
  • 5
  • 41
  • 60
1
vote
5 answers

How to check if imageView is not nil in swift?

I have a fatal error while unwrapping an optional value in swift. I have a profile ViewController with a background image and an avatar image witch are the same. When the user has not an image set, i ve got this fatal error, instead i would like to…
jmcastel
  • 1,365
  • 7
  • 17
  • 34
1
vote
3 answers

What the difference between using or not using "!" in Swift?

The ! mark is used for the optional value in Swift to extract the wrapped value of it, but the value can be extracted without the !: var optionalString: String? = "optionalString" println(optionalString) another example using the !: var…
twlkyao
  • 14,302
  • 7
  • 27
  • 44
1
vote
2 answers

Swift Optional Type Syntax

According to Apple's Swift guide, when you have the following Array and the following function to search through an Array: let namesArray = ["John", "Lisa", "Bill", "Jennifer"] // Note this function is set-up to return an OPTIONAL Int: func…
sirab333
  • 3,662
  • 8
  • 41
  • 54
1
vote
1 answer

What is the intended use of optional variable/constant in swift

In objC, NSString *stringValue = @"123s"; NSInteger *intValue = [stringValue integerValue]; NSLog(@"intergerValue %@",intValue); if(!intValue) { NSLog(@"caught null object"); } else { // Do appropriate operation with the not null…
Suresh Kumar Durairaj
  • 2,104
  • 16
  • 24
1
vote
1 answer

Not declaring a property as optional results in "property not initialized at super.init()" error

I have written the following code for a protocol. I pass the the reference to the delegate variable and use it to call interface/protocol functions. However, if I put ? for declaring the Protocol object, no error is generated. If I don't, it gives…
user2498079
  • 2,872
  • 8
  • 32
  • 60
0
votes
4 answers

Alternatives to optional (VB) parameters in C#?

I have been told that there is no such thing as optional parameters in C#. But you can use overload functionality and input varibles with default values such as: void Person(string name, int age) void Person(string name) and void Person(string…
0
votes
1 answer

Check Non Optional value for nil

I'm creating a function in swift to check the if non optional value return nil. My aim is just to handle that exception and avoid app crash for unexpected nil values. I have two variables in my class: // My Class variables var compulsoryValue:…
developer
  • 668
  • 1
  • 6
  • 24
0
votes
1 answer

Optional variables in API call swift

I have a variable like below let faces: [(face: Smiley, label: UILabel)] = [ (Smiley(icon: .worse), UILabel()), (Smiley(icon: .bad), UILabel()), (Smiley(icon: .ok), UILabel()), (Smiley(icon: .good), UILabel()), (Smiley(icon:…
j.krissa
  • 223
  • 5
  • 21
0
votes
2 answers

Difference between two optional syntaxes in Swift

What is difference between self?.profile!.id! and (self?.profile!.id!)! XCode converts first to second.
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
0
votes
2 answers

Swift Optional Dictionary [String: String?] unwrapping error

So here I have a basic setup var preferenceSpecification = [String : String?]() preferenceSpecification["Key"] = "Some Key" preferenceSpecification["Some Key"] = nil preferenceSpecification["DefaultValue"] = "Some…
RodolfoAntonici
  • 1,555
  • 21
  • 34
0
votes
1 answer

Global variable and optional binding in Swift

I have some quite simple doubt regarding optional binding,global variable & wrapping and unwrapping . Since I am new to SWIFT, its very important to understand the tits & bits of their concepts. 1) In Swift if I declare a global variable, I have 2…
G.Abhisek
  • 1,034
  • 11
  • 44
0
votes
1 answer

Why is my Swift string variable returning nil?

I am trying to use Parse to create a simple tableview that triggers the URLs of PDF documents that I have uploaded to the Parse Cloud. In the code below, my variable thePDFFile does output a URL correctly to the console. But when I try to return…
user3140521
  • 133
  • 11