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 Build an Interactive Story App with Swift Refactoring Our Code Immediately Executing Anonymous Functions

Haifa Bassam
Haifa Bassam
10,333 Points

Error: Expression is not assignable: function call returns immutable value

when I write this code:

let storyLabel = UILabel() = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.numberOfLines = 0
    return label
}()

I get an error:

Expression is not assignable: function call returns immutable value

Any clues why?

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Your issue is on the first line:

let storyLabel = UILabel() = {

It looks like you are trying to set the type of the storyLabel constant to UILabel. Instead you are assigning the constant to a new UILabel instance and then trying to assign the result of the UILabel initializer to the closure result.

To set the type pf the constant, you should use this:

let storyLabel: UILabel = {