need simple help with my swift code :D

Discussion in 'Mac Programming' started by jonpeter, Feb 7, 2015.

  1. jonpeter macrumors member

    Joined:
    Sep 19, 2013
    Location:
    MI
    #1
    So I have a struct set up for numerous instances i plan on putting in my array.
    anyways, in my struct
    I have
    "var title: String
    Var type: String
    var pic: UIImageView"

    and in my instance i Keep getting an error revolving around the image portion of the struct.

    This is my code in the view controller btw. let restaurant3 = restaurantmodel(restitle: "space dungeon", restype: "cotton candy", resimage: "space.png")

    Am i setting up my "pic" variable wrong?

    thanks for any help.... beginning to learn swift...
     
  2. joshlalonde, Feb 10, 2015
    Last edited: Feb 10, 2015

    joshlalonde macrumors 6502

    joshlalonde

    Joined:
    Jul 12, 2014
    Location:
    Canada
    #2
    Uh, i think there's a code tag.
    Try formatting it with that?

    Code:
    struct restaurant_model { // Struct
          var title: String
          var type: String
          var picName: String
    }
    
    let restaurant3 = restaurant_model(title: "space dungeon", type: "cotton candy", picName: "space.img") // Restaurant 3 
    
    I fixed up some mistakes. You changed title to restitle, for example.
    Also, you can define the picName as a string, and then use it like:

    Code:
    
    let image = UIImage(named: restaurant3.picName);
    let imageView = UIImageView(image: image!)
    
    
    I don't know much about Swift but that's what I found from my digging.
     
  3. theSeb macrumors 604

    theSeb

    Joined:
    Aug 10, 2010
    Location:
    Poole, England
    #3
    You have title, type and pic. In the next line you're trying to use restitle, restype and resimage. That is your first issue.

    The second issue is that pic is of type UIImageView. You're trying to use a string "space.png" instead of UIImageView. I am pretty sure you intended UIImage, or pic should have been a string. You need to make that decision.

    Having said that, stick to learning Swift as a pure programming language, before attempting to use Apple's classes designed to create applications. What you're trying to do is like trying to learn Russian with a Russian copy of The Brothers Karamazov
     
  4. jonpeter thread starter macrumors member

    Joined:
    Sep 19, 2013
    Location:
    MI
    #4
    PHP:
    I was able to figure that in my class, I had to create
    "var resimage: UIImage?"
    PHP:
    and in my code, I had to use
    "resimage: UIImage(named: "space.png")"
     

Share This Page