Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Drew Hartsfield
Drew Hartsfield
2,212 Points

Swift Giving me an error and I don't know why??

error down below?

struct Amino {
    var firstAmino: String
    var secondAmino: String
    var thirdAmino: String

init(firstAmino: String, secondAmino: String, thirdAmino: String ) {
    self.firstAmino = firstAmino
    self.secondAmino = secondAmino
    self.thirdAmino = thirdAmino
}

func secondAminoAcid() -> String {
    return; if(self.firstAmino = "G") { // get an error saying cannot assign to 'firstAmino' in self
        println("C")
    }
    else if(self.firstAmino = "C") {
        println("G")
    }
    else if(self.firstAmino = "A"){
        println("U")
    }
    else if(self.firstAmino = "T"){
        println("A")
    }
}
}
var blah = Amino(firstAmino: "G", secondAmino: "C", thirdAmino: "A")
blah.secondAminoAcid()

3 Answers

Hi Drew,

I edited your post to make the code clearer.

I've not done this course so am not sure what you're trying to achieve.

However, you have a return right at the beginning of your func. That will immediately stop the function executing.

Next, in your conditional tests, you are assigning values to variables. Are you trying to compare? In which case, you need to use a double equals sign. So to test if firstAmino holds the value "G" you need to use if(self.firstAmino == "G").

I hope that makes sense.

Steve.

No problem!

Steve.

Drew Hartsfield
Drew Hartsfield
2,212 Points

Where do I place the return than?

Do you return the result after each 'if' rather than just outputting a 'println'?

I've not seen the challenge so don't really know.

So instead of each 'println("letter")' do 'return("letter")'

If not, let me know and I'll have a look tomorrow.

Steve.

Drew Hartsfield
Drew Hartsfield
2,212 Points

So this isn't a challenge but just a problem I had when coding my own function. Now I replaced the println with return which worked. Thanks very much, but now I am getting an error that says missing return in a function expected to return a string.

You need to have a return in each of your if tests. You then need a default return after them all in case none of the conditions are ever met. The code doesn't know that those are the only options. So add a 'return "";' after all your tests.

A select/case is the better form to use for multiple tests like this, to be honest.

Steve.