0

I am studying XCode7.3. The "?" and "!" always make me confused.

I have code like below.

The lines name : name, type : type and image : image displaying error message :

Value of optional type 'String?' not unwrapped, did you mean to use '!' or '?'?

@IBAction func unwindToHomeScreen( segue : UIStoryboardSegue ) {
    if let addRestaurantController = segue.sourceViewController as? AddRestaurantController {
        let name = addRestaurantController.name
        let location = addRestaurantController.location
        let type = addRestaurantController.type
        let isVisited = addRestaurantController.isVisited
        let image = addRestaurantController.imageView

        restaurants.append( Restaurant(
            name : name,
            type : type,
            location : location,
            phoneNumber : "UNKNOW",
            image : image,
            isVisited : isVisited? ?? false
        ) )

        print( addRestaurantController.imageView )
    }
}

I modify code to name : name! or name : name?, it still doesn't work. How can I fix it?

Wain
  • 118,658
  • 15
  • 128
  • 151
LCB
  • 971
  • 9
  • 22
  • Could you please modify your code. And post it correctly? –  Jul 04 '16 at 05:55
  • Friend you can study Swift 3 in Xcode 7, For that you have to use Xcode 8.0, which is currently in beta. Also I think that this question does not have anything to do with Swift 3.0 –  Jul 04 '16 at 05:59
  • @AlvinVarghese. thank you very much, I thought my swift version is 3.0. – LCB Jul 04 '16 at 06:20
  • @AlvinVarghese of course you can modify the code. but the style of the question was broken, i changed back, you should modify it again if you want. – LCB Jul 04 '16 at 06:21
  • 2
    Consider to use less optionals at all. For example in practice I've never seen a restaurant without name and location. And `visited` is either false or true, there no rational reason to use an optional. – vadian Jul 04 '16 at 06:38
  • You really need to define `doesnt work`... – Wain Jul 04 '16 at 06:41
  • [This Q&A](http://stackoverflow.com/questions/32170456/) contains a very good canonical answer covering the basics of optionals, have a look at it. I also vote to close this question as a duplicate of the fore-mentioned Q&A. – dfrib Jul 04 '16 at 07:51

2 Answers2

0

I think this will solve your problem.

If addRestaurantController.location, addRestaurantController.name and addRestaurantController.type are optional.

@IBAction func unwindToHomeScreen( segue : UIStoryboardSegue ) {

    if let addRestaurantController = segue.sourceViewController as? AddRestaurantController , name = addRestaurantController.name, location = addRestaurantController.location, type = addRestaurantController.type, isVisited = addRestaurantController.isVisited, image = addRestaurantController.imageView
    {
        restaurants.append( Restaurant(
            name : name,
            type : type,
            location : location,
            phoneNumber : "UNKNOW",
            image : image,
            isVisited : isVisited? ?? false
            ) )
    }
}

You haven't specify that whether addRestaurantController.location, addRestaurantController.name and addRestaurantController.type are optional or not. And you randomly submitted your question without even analysing it properly.

Please give more value to other people's time. And ask meaningful questions. Read more about guidelines for a Stackoverflow question.

0

There might be few reasons, but first I'd suggest you to read about optionals (?) and explicitly unwrapped optionals (!) here https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

As far as the problem, it is most likely that optionality of variable name in Restaurant is defined differently comparing to local definition. While locally variable name is defined to be optional, e.g. let name: String?, Restaurant expects it to be non optional (probably it defined as let name: String (note no ? at the end)). That mean that you need to unwrap optional value to pass it to the Restaurant (please see the link above for ways of unwrapping, there are few depending on your use-case)

If you are switching to Swift 3, keep in mind that behavior of explicitly unwrapped optionals changed. For motivation and in depth description, please read this proposal https://github.com/apple/swift-evolution/blob/master/proposals/0054-abolish-iuo.md

But as it's been pointed out, Swift 3 can not be compiled in Xcode 7, you need to download beta of Xcode 8 for that

Sash Zats
  • 5,376
  • 2
  • 28
  • 42