Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. iOS
  4. How do I unwrap an optional within a struct and use an if statement to print the optional?

How do I unwrap an optional within a struct and use an if statement to print the optional?

Scheduled Pinned Locked Moved iOS
questionswifthelptutorial
2 Posts 2 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WilliamChen7
    wrote on last edited by
    #1

    I am getting an error message for my use of nil and I am confused on how to properly unwrap the optional while placing it in an if statement to print out a string.

    struct DatingProfile{
    var Name: String
    var Age: Int
    var City: String
    var FavoriteMovie: String?
    var Hobbies: String?

    }
    
    let myDatingProfile = DatingProfile(Name: "William", Age: 30, City: "North Hollywood", FavoriteMovie: nil , Hobbies: "Basketball")
    
    if let myFavoriteMovie = FavoriteMovie = nil{
      print("\\(Name) does not have a favorite movie")
       
    }else {
      
        print(myDatingProfile.FavoriteMovie)
      }
      
    }
    

    exit status 1 main.swift:12:26: error: use of unresolved identifier 'FavoriteMovie' if let myFavoriteMovie = FavoriteMovie = nil{ ^~~~~~~~~~~~~ main.swift:13:12: error: use of unresolved identifier 'Name' print("\(Name) does not have a favorite movie") ^~~~ main.swift:17:11: warning: expression implicitly coerced from 'String?' to 'Any' print(myDatingProfile.FavoriteMovie) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.swift:17:27: note: provide a default value to avoid this warning print(myDatingProfile.FavoriteMovie) ~~~~^ ?? <#default value#> main.swift:17:27: note: force-unwrap the value to avoid this warning print(myDatingProfile.FavoriteMovie) ~~~~^ ! main.swift:17:27: note: explicitly cast to 'Any' with 'as Any' to silence this warning print(myDatingProfile.FavoriteMovie) ~~~~^ as Any main.swift:20:1: error: extraneous '}' at top level } ^

    Richard DeemingR 1 Reply Last reply
    0
    • W WilliamChen7

      I am getting an error message for my use of nil and I am confused on how to properly unwrap the optional while placing it in an if statement to print out a string.

      struct DatingProfile{
      var Name: String
      var Age: Int
      var City: String
      var FavoriteMovie: String?
      var Hobbies: String?

      }
      
      let myDatingProfile = DatingProfile(Name: "William", Age: 30, City: "North Hollywood", FavoriteMovie: nil , Hobbies: "Basketball")
      
      if let myFavoriteMovie = FavoriteMovie = nil{
        print("\\(Name) does not have a favorite movie")
         
      }else {
        
          print(myDatingProfile.FavoriteMovie)
        }
        
      }
      

      exit status 1 main.swift:12:26: error: use of unresolved identifier 'FavoriteMovie' if let myFavoriteMovie = FavoriteMovie = nil{ ^~~~~~~~~~~~~ main.swift:13:12: error: use of unresolved identifier 'Name' print("\(Name) does not have a favorite movie") ^~~~ main.swift:17:11: warning: expression implicitly coerced from 'String?' to 'Any' print(myDatingProfile.FavoriteMovie) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.swift:17:27: note: provide a default value to avoid this warning print(myDatingProfile.FavoriteMovie) ~~~~^ ?? <#default value#> main.swift:17:27: note: force-unwrap the value to avoid this warning print(myDatingProfile.FavoriteMovie) ~~~~^ ! main.swift:17:27: note: explicitly cast to 'Any' with 'as Any' to silence this warning print(myDatingProfile.FavoriteMovie) ~~~~^ as Any main.swift:20:1: error: extraneous '}' at top level } ^

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      I don't know Swift, but my guess would be something like:

      if let myFavoriteMovie = myDatingProfile.FavoriteMovie {
      print(myFavoriteMovie)
      }
      else {
      print("\(myDatingProfile.Name) does not have a favorite movie")
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups