Questions tagged [optional-values]

79 questions
35
votes
3 answers

Optionally adding items to a Scala Map

I am looking for an idiomatic solution to this problem. I am building a val Scala (immutable) Map and would like to optionally add one or more items: val aMap = Map(key1 -> value1, key2 -> value2, (if (condition) (key3 -> value3) else…
Ralph
  • 31,584
  • 38
  • 145
  • 282
18
votes
2 answers

Double exclamation !! mark in Swift?

I know the definition for a single exclamation mark, but two? I was coding today and the compiler "force" me to add one more ! to my sentence: mySignal.subscribeNext({ (value: AnyObject!) -> () in let list: RACSequence =…
FelipeDev.-
  • 3,113
  • 3
  • 22
  • 32
8
votes
2 answers

How do I get interdependent options?

I want to do something like foo[OptionsPattern[]] := OptionValue[b] Options[foo] = {a -> 0, b :> OptionValue[a]}; foo[a -> 1] and have Mathematica give me 1, instead of 0. Is there a better way to do this than foo[OptionsPattern[]] := ( …
Jason Gross
  • 5,928
  • 1
  • 26
  • 53
7
votes
2 answers

Swift error while downcasting 'Any'

The following code is almost exact replica from Apple Documentation and compiles without errors: guard let firstItem = (rawItems! as? Array>)?.first else { throw AnError() } let identityRef =…
cyanide
  • 3,885
  • 3
  • 25
  • 33
7
votes
2 answers

swift: if-let with parallel assignment

I'm trying to make a simple if-let statement with more than one value. The if block should be executed only if all optional vars are non-nil, plus they should be assigned to the new let-vars (constants?) that live only inside the if block, just like…
4
votes
2 answers

Set UIButton Image in Swift - currently unwraps as nil

I am having a difficult time setting an image of a UIButton. I have checked other posts on how to do this, and it seems straightforward enough (but then, here I am). In my program, the image that the button displays depends on trip.weather. I am…
turbs
  • 85
  • 8
4
votes
3 answers

Difference between optional values in swift?

What is the difference between: var title:String? = "Title" //1 var title:String! = "Title" //2 var title:String = "Title" //3 What am I saying if I were to set title in each way and am I forced to unwrap each variable in a different way?
jshah
  • 1,599
  • 2
  • 17
  • 38
3
votes
1 answer

Realm object with NSData variable , unexpectedly found nil while unwrapping an Optional value

I get an Error after adding a NSData variable to my Realm Model: fatal error: unexpectedly found nil while unwrapping an Optional value This Error doesn't appear when im not using the NSData value. This is my simple Item (Item.swift) class…
Korken55
  • 233
  • 1
  • 3
  • 14
3
votes
1 answer

Default values for function parameters where value passed in is nil

Is there a neat way to combine default function parameter values and optionals, so the parameter takes on the value of the supplied default when the parameter is specified in the function call, but it's value is nil? For example: class MyObj { …
Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
3
votes
3 answers

What's the difference between testing an optional for "nil" and ".None"?

I see everywhere in swift language, people testing for optional values with if statements so they can safely force unwrap, in 2 ways: if optionalValue != .None { ... some code ... } or if optionalValue != nil { ... some code…
Fero
  • 567
  • 8
  • 25
3
votes
1 answer

Swift optional chaining with NSDictionary

Please help to remake this if let field = parent_obj?.getFieldForCode(code) { if let stored_value = field["value"] as? String { into optional chaining syntax in single line. I tried to do it like this: let stored_value =…
John Kakon
  • 2,531
  • 4
  • 24
  • 28
3
votes
2 answers

Python - Optional list values - A more pythonic way?

I'd like to know if there is a more pythonic way to declare a list with an optional value? title = data.get('title') label = data.get('label') or None if label: parent = [title, label] else: parent = [title] Thanks in advance.
user937284
  • 2,454
  • 6
  • 25
  • 29
2
votes
2 answers

Helm Conditional template for Resource request

I want to implement a conditional template for Deployment resources through helm which can be enabled or disabled as per the environment. Something like the below which in not working. Or can we achieve the same through a different…
2
votes
1 answer

Regex: Match optional string to group

I have a file describing objects in which some properties of the object are optional. For example (color is…
Riddick
  • 31
  • 2
2
votes
2 answers

Scala - Sort by Optional fields, with None value last

I have the following class : final case class PowerDetails ( [...], label: String, title: String, category: String, idCategory: Option[String], numCategory: Option[Int] ) I need to sort a list of PowerDetails like this : Elements…
sewey
  • 51
  • 3
  • 9
1
2 3 4 5 6