Questions tagged [optional-variables]
36 questions
                    
                    103
                    
            votes
                
                9 answers
            
        Downcasting optionals in Swift: as? Type, or as! Type?
Given the following in Swift:
var optionalString: String?
let dict = NSDictionary()
What is the practical difference between the following two statements:
optionalString = dict.objectForKey("SomeKey") as? String
vs
optionalString =…
         
    
    
        sdduursma
        
- 2,651
- 3
- 16
- 16
                    30
                    
            votes
                
                2 answers
            
        Multiple optional parameters calling function
Assume that i have a function like this below
It takes 3 parameters and 2 have optional values
private void  myfunc (int a, int b=2, int c=3)
{
  //do some stuff here related to a,b,c
}
now i want to call this function like below how possible…
         
    
    
        Furkan Gözükara
        
- 22,964
- 77
- 205
- 342
                    7
                    
            votes
                
                3 answers
            
        VB.NET := Operator
What does the following mean?
Class.Function(variable := 1 + 1)
What is this operator called, and what does it do?  
         
    
    
        Kevin
        
- 13,044
- 11
- 55
- 76
                    6
                    
            votes
                
                2 answers
            
        Swift optional initialisation
It's my understanding that 
var perhapsInt : Int?
This is automatically set to a .None value. And the below code snippet confirms that (no compiler errors)
class MyClass {
    var num1: Int = 0
    var num2: Int?
    init(num1: Int) {
       …
         
    
    
        hdsenevi
        
- 953
- 2
- 14
- 27
                    6
                    
            votes
                
                3 answers
            
        Swift 2: is there a short syntax to conditionally set a non-optional variable from an optional variable?
Sometimes I find myself writing swift 2 code like this:
class Test {
    var optionalInt: Int?
    var nonOptionalInt: Int = 0
    func test() {
        if let i = optionalInt {
            nonOptionalInt = i
        }
        // do more stuff
   …
         
    
    
        Gerd Castan
        
- 6,275
- 3
- 44
- 89
                    5
                    
            votes
                
                4 answers
            
        Best way to encapsulate "optional" fields within a struct generically in C++?
I have many concrete-structs and I want to designate fields as optional (present or not-present).   Just wondering what ideas people have for achieving this.  Here is an example struct (fields can be other structs as well, even vectors of structs):
…
         
    
    
        chriskirk
        
- 761
- 1
- 11
- 22
                    5
                    
            votes
                
                2 answers
            
        boost::optional<> in a union?
I have an optional POD struct that will be contained inside a union.
boost::optional<> holds its type by value, so I thought this could work:
union helper
{
    int foo;
    struct 
    {
        char basic_info;
        struct details {
           …
         
    
    
        mskfisher
        
- 3,291
- 4
- 35
- 48
                    5
                    
            votes
                
                3 answers
            
        Checking optional isEqualToString
I have a case where I want to check if an optional I have is Equal to a string.
First I have to unwrap it to check if it exists, then I want to check if it is equal to another string. However that gives me a problem where I have to write the same…
         
    
    
        fisher
        
- 1,286
- 16
- 29
                    5
                    
            votes
                
                3 answers
            
        Traverse view controller hierarchy in Swift
I would like to traverse the view controller hierarchy in Swift and find a particular class. Here is the code: 
extension UIViewController{
    func traverseAndFindClass() -> UIViewController?{
        var parentController =… 
         
    
    
        the_critic
        
- 12,720
- 19
- 67
- 115
                    5
                    
            votes
                
                1 answer
            
        Generating values for empty attributes
I am trying to fill the empty spaces generated by the use of Optional in the SPARQL query language. Are there any ways that I can achieve this? 
The use of !bound on the optional variable generates true or false, but I want to fill the cells with my…
         
    
    
        honour nwagwu
        
- 51
- 3
                    4
                    
            votes
                
                2 answers
            
        Swift Optionals : What if i need to change an non - optional type to optional
I had designed an custom object which was sure to return a value. Now the whole app is build around that custom object. Now conditions have appeared which can cause the custom object to be nil.
What do I do . Change the type to optional ? and change…
         
    
    
        Ankish Jain
        
- 11,305
- 5
- 36
- 34
                    4
                    
            votes
                
                1 answer
            
        Optional Chaining or ternary expression in Swift?
Is there a more efficient, readable or more modern way of writing the following?
let currentColor:UIColor = view.backgroundColor != nil 
  ? view.backgroundColor! // forced unwrapping
  : UIColor.whiteColor() // fallback value
I don't mind using a…
         
    
    
        Armstrongest
        
- 15,181
- 13
- 67
- 106
                    2
                    
            votes
                
                1 answer
            
        laravel route variables passing null values give url error
I have a route that has 3 optional variables declared as null in Controller
Route::get('gegonota/{gid?}/{cid?}/{nid?}', [
'uses' => 'GegonosController@index', 
'as' => 'gegonota'
]);
and even i change the order of the parameter the problem still…
         
    
    
        gasprice admin
        
- 143
- 2
- 11
                    2
                    
            votes
                
                2 answers
            
        Regular expression strings with consecutive variables
I'm fairly new to regex (regular expressions) and need a little bit of help formulating a string. I understand it for the most part but got stumped when the text I needed to match had variables followed by an optional phrase.
Say the text is…
         
    
    
        spatel4140
        
- 383
- 3
- 10
                    2
                    
            votes
                
                2 answers
            
        How to handle optional variables of an object in Java?
For my trading program, I have a Merchant class. A given Merchant object may or may not have a particular special quality or bundle of special qualities. For example, one Merchant object may have the Stockbroker quality, another Merchant may have…
         
    
    
        Arvanem
        
- 1,043
- 12
- 22