optional-variables

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

笑着哭i 提交于 2020-01-03 02:55:15
问题 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 object } prints " interValue (null) " " caught null object " and the binding is done safely by using !(not) operator inside if condition... But whereas, in swift the equivalent snippet using optional variable is var normalValue : String = "123s" var optionalValue =

Traverse view controller hierarchy in Swift

核能气质少年 提交于 2020-01-02 07:31:18
问题 I would like to traverse the view controller hierarchy in Swift and find a particular class. Here is the code: extension UIViewController{ func traverseAndFindClass<T : UIViewController>() -> UIViewController?{ var parentController = self.parentViewController as? T? ^ | | // Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController' while(parentController != nil){ parentController = parentController!.parentViewController } return parentController

Generating values for empty attributes

那年仲夏 提交于 2019-12-23 16:01:54
问题 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 own values such as "?" or "unknown". 回答1: Perhaps you could use one of the following constructs... COALESCE(?c, "unknown") Source: http://www.w3.org/TR/sparql11-query/#func-coalesce or IF(bound(?c), ?c, "unknown") Source: http://www.w3.org/TR

How to check if imageView is not nil in swift?

*爱你&永不变心* 提交于 2019-12-13 00:06:41
问题 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 add a "by default image Shape". How could i check if image isn't nil ? This is my code : var currentUser = PFUser.currentUser() let User = currentUser as PFUser let userImage:PFFile = User["profileImage"] as PFFile { userImage

Swift Optional Dictionary [String: String?] unwrapping error

泄露秘密 提交于 2019-12-11 23:33:51
问题 So here I have a basic setup var preferenceSpecification = [String : String?]() preferenceSpecification["Key"] = "Some Key" preferenceSpecification["Some Key"] = nil preferenceSpecification["DefaultValue"] = "Some DefaultValue" print(preferenceSpecification) var defaultsToRegister = [String : String]() if let key = preferenceSpecification["Key"], let defaultValueKey = preferenceSpecification["DefaultValue"] { defaultsToRegister[key] = preferenceSpecification[defaultValueKey]! } But the error

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

我的梦境 提交于 2019-12-07 10:56:28
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 object } prints " interValue (null) " " caught null object " and the binding is done safely by using !(not) operator inside if condition... But whereas, in swift the equivalent snippet using optional variable is var normalValue : String = "123s" var optionalValue = normalValue.toInt() println("optionvalue \(optionalValue)") if optionalValue { // Do appropriate

Best way to encapsulate “optional” fields within a struct generically in C++?

和自甴很熟 提交于 2019-12-07 04:10:04
问题 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): struct LogonMessage_t { Header_t header; // this points to another struct containing all primitives std::string username; std::string password; std::vector<LogonOption_t> LogonOptions; int subaccountid; std::string Text; } I'd like to default all

What the difference between using or not using “!” in Swift?

只谈情不闲聊 提交于 2019-12-06 11:25:59
问题 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 optionalString: String? = "optionalString" println(optionalString!) both the codes above get the right value, so I wonder what's the difference between using and not using ! , is it just for detect an error at runtime if the optional value is nil or

Traverse view controller hierarchy in Swift

China☆狼群 提交于 2019-12-05 18:39:43
I would like to traverse the view controller hierarchy in Swift and find a particular class. Here is the code: extension UIViewController{ func traverseAndFindClass<T : UIViewController>() -> UIViewController?{ var parentController = self.parentViewController as? T? ^ | | // Error: Could not find a user-defined conversion from type 'UIViewController?' to type 'UIViewController' while(parentController != nil){ parentController = parentController!.parentViewController } return parentController } } Now, I know that the parentViewController property returns an optional UIViewController, but I do

Best way to encapsulate “optional” fields within a struct generically in C++?

允我心安 提交于 2019-12-05 10:21:23
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): struct LogonMessage_t { Header_t header; // this points to another struct containing all primitives std::string username; std::string password; std::vector<LogonOption_t> LogonOptions; int subaccountid; std::string Text; } I'd like to default all fields to not-present and enable them one by one, perhaps in their setters. Since these structs are