iOS Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 1

Juliano Jakymiu
Juliano Jakymiu
2,040 Points

Code could not be compiled, but preview is empty

struct Tag { let name: String }

struct Post {

let title: String
let author: String
let tag = Tag(name:"swift")

func description() -> String {
    return "\(title) by \(author). Filed under \(tag)"
}

}

let firstPost = Post(title:"IOSDevelopment", author:"Apple") let postDescription = firstPost.description()

Why is this not working? Or even better, if tag of type Tag, which in itself is a String, how do I feel the parameters? If I do not assign a value to tag under the struct Post, and later on try to create firstPost = Post (title: "xxx", author: "xxl", tag "123), I get an error saying that tag is not a String type, but a Tag type. Tag type in itself is a String type. I am super confused...

structs.swift
struct Tag {
    let name: String
}

struct Post {

    let title: String
    let author: String
    let tag = Tag(name:"swift")

    func description() -> String {
        return "\(title) by \(author). Filed under \(tag)"
    }

}

let firstPost = Post(title:"IOSDevelopment", author:"Apple")
let postDescription = firstPost.description()
Jonathan Fernandez
Jonathan Fernandez
4,044 Points

The property "tag" you declared in your stuct "Post", is an "instance of Tag". Thereby, it is of type Tag not of string. Think of enums, structs, and class as a type in its own-self. However, rather than being a type like Int or String, it is the type given to any instance of it, hence those three are known as object "types."

If you want to access your name in your tag "instance" you created. then in your string interpolation replace "title" with tag.name. By doing so you are accessing the name property declared in your tag instance you created.

3 Answers

You should not instantiate the Tag property inside the Post struct.

struct Tag {
  let name: String
}

struct Post {
  let title: String
  let author: String
  let tag: Tag
}


let firstPost = Post(title: "Swift", author: "Apple", tag: Tag(name: "Programming"))
Juliano Jakymiu
Juliano Jakymiu
2,040 Points

Hi guys, still nothing. If I do as Amazon Web said, now the "Tag(name: "XZY")" is part of the interpolation and I still get and error saying to match the description using interpolation

struct Tag { let name: String }

struct Post { let title: String let author: String let tag: Tag

func description() -> String { return "(title) by (author). Filed under (tag)" }

}

let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift")) let postDescription = firstPost.description()

So here is the exact answer. You are should not just call the property tag but you should call the property of the tag property which is the name

struct Tag {
  let name: String
}

struct Post {
  let title: String
  let author: String
  let tag: Tag

  func description() -> String {
    return "\(title) by \(author). Filed under \(tag.name)"
  }
}


let firstPost = Post(title: "Swift", author: "Apple", tag: Tag(name: "Programming"))
let postDescription = firstPost.description()