optional

Expression implicitly coerced from 'String?' to Any [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-30 01:34:25
问题 This question already has answers here : Swift 3: Expression implicitly coerced from 'UIView?' to Any (4 answers) Closed 3 years ago . I have some error like this "Expression implicitly coerced from String? to Any " this is my code : func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FIRApp.configure() FIRAuth.auth()?.signIn(withEmail:

fatal error: unexpectedly found nil while unwrapping an Optional value - why?

情到浓时终转凉″ 提交于 2019-12-29 09:32:27
问题 I'm pretty new to coding in Swift and I'm not too sure what's happening here - can anyone help? Thanks import UIKit class SecondViewController: UIViewController { var toDoItems:[String] = [] @IBOutlet weak var toDoItem: UITextField! @IBAction func addItem(sender: AnyObject) { toDoItems.append(toDoItem.text) fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) println(toDoItems) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after

fatal error: unexpectedly found nil while unwrapping an Optional value - why?

对着背影说爱祢 提交于 2019-12-29 09:31:11
问题 I'm pretty new to coding in Swift and I'm not too sure what's happening here - can anyone help? Thanks import UIKit class SecondViewController: UIViewController { var toDoItems:[String] = [] @IBOutlet weak var toDoItem: UITextField! @IBAction func addItem(sender: AnyObject) { toDoItems.append(toDoItem.text) fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) println(toDoItems) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after

Swift if let evaluates successfully on Optional(nil)

社会主义新天地 提交于 2019-12-29 08:42:09
问题 I have a custom object called Field. I basically use it to define a single field in a form. class Field { var name: String var value: Any? // initializers here... } When the user submits the form, I validate each of the Field objects to make sure they contain valid values. Some fields aren't required so I sometimes deliberately set nil to the value property like this: field.value = nil This seems to pose a problem when I use an if-let to determine whether a field is nil or not. if let value =

Why do Objective-C APIs return implicitly unwrapped optionals?

那年仲夏 提交于 2019-12-29 05:56:10
问题 I am rather perplexed by this. If we take the method cellForRowAtIndexPath: in UITableView for example, it's method signature is: func cellForRowAtIndexPath(_ indexPath: NSIndexPath!) -> UITableViewCell! And its return value is: An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range. That sounds like the perfect reason to use a standard optional. In fact, since all pointer based types in Objective-C can be nil... it seems to make sense that

Java 8's orElse not working as expected

筅森魡賤 提交于 2019-12-28 06:41:38
问题 Consider the following method which returns a field if it exists or recursively calls itself until the field is found: private Field getField(Class<?> clazz, String p) { Optional<Field> field = Arrays.stream(clazz.getDeclaredFields()) .filter(f -> p.equals(f.getName())) .findFirst(); return field.isPresent() ? field.get() : getField(clazz.getSuperclass(), p); } While this works, I thought I could shorten it to: private Field getField(Class<?> clazz, String p) { return Arrays.stream(clazz

How to make generics in collection type constraint?

我是研究僧i 提交于 2019-12-28 04:33:07
问题 I have been trying to extract non-nil values from the String array. Like below. But, my senior wants it to be able to extract non-nil values from other types too. I read, generics could help me for handling different types. How can I use generics so that I get to use following like extension to work with other types too? getNonNil must return the extracted non-nil values of the specific type (i.e. if array is [String?] it must return [String], returns [Int] if [Int?] ) Because I have to do

Why is main window of type double optional?

隐身守侯 提交于 2019-12-27 23:36:40
问题 When accessing UIapplication's main window it is returned as a UIWindow?? let view = UIApplication.sharedApplication().delegate?.window // view:UIWindow?? Why is it returning as a double optional and what does it mean and if put into a if let should I add one ! after it? if let view = UIApplication.sharedApplication().delegate?.window! My first though was to replace ? with a ! after delegate but that was not the solution. 回答1: @matt has the details, but there is a (somewhat horrible, somewhat

Cannot use optional chaining on non-optional value of type 'Auth'

跟風遠走 提交于 2019-12-25 21:08:37
问题 var loggedInUser: User? let storageRef = Storage.storage().reference() let databaseRef = Database.database().reference() // structure definition goes here override func viewDidLoad() { super.viewDidLoad() self.loggedInUser = Auth.auth()?.currentUser//Cannot use optional chaining on non-optional value of type 'Auth' self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:DataSnapshot) in //'observeSingleEventOfType(_:withBlock:)' has

Cannot use optional chaining on non-optional value of type 'Auth'

为君一笑 提交于 2019-12-25 21:07:36
问题 var loggedInUser: User? let storageRef = Storage.storage().reference() let databaseRef = Database.database().reference() // structure definition goes here override func viewDidLoad() { super.viewDidLoad() self.loggedInUser = Auth.auth()?.currentUser//Cannot use optional chaining on non-optional value of type 'Auth' self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:DataSnapshot) in //'observeSingleEventOfType(_:withBlock:)' has