Xcode 6.1 - Console error for struct instances
I am new to the programming world and I choose "Swift" for my first programming language. Everything is still great, but now I am learning about structs and instances, how to update them, etc. and I get a cryptic console error and I can't move on with my project.
So the problem.
I created a structure called "Tiger.swift". There are properties for this structure like name, age, etc. In "viewController" I created a new variable myTiger and tried to update its properties, and after my generated println command something strange appears on the console. It compiles and tells me that "the build was successful" but something like this appears in the console.
"My Tiger name: Tigger, age 3, age 3, and image is optional ()"
And this strange thing is "Optional." Because if I later want to update my instances, it won't compile and show me:
"My tiger's name is Tiger, age 3, age 3, and image is Optional () fatal error: unexpectedly found nil while expanding optional value (lldb)"
There is a link to the Git repository ( https://github.com/llinards/lionsandtigers ) unless there is a quick fix or a small mistake I accidentally made.
I would appreciate any help from you!
Thank!
source to share
I took a close look at your code:
- I changed the property of
image
your Tiger structure to be optional because you don't have a template or placeholder image (if you have one, you can set an initial value for that image). - Your storyboard (including your IBOutlets) is very beautiful ... I don't know how it happened, I've never seen anything like it before ...
- I have uploaded a working copy of your project here (It's hard to show that there is IB related stuff here, I just removed and re-added your labels and views).
-
I made the corresponding code changes:
In the Tiger model object (Tiger.swift), I changed the last line from
var image = UIImage(named:"")
to
var image: UIImage?
The first error you encountered was what
UUImage(named:"")
returnsnil
. In Swift, an optional type should never be nil.In your
ViewController
a, the tiger initialization has changed toTiger(age: 3, name: "Tigger", breed: "TOne", image: UIImage(named: "t1.jpg"))
Your call
println()
shows:My Tiger name: Tigger, age 3, age 3, and image optional (UIImage: 0x7f8d78f43c90)
This is correct since your image is now wrapped in optional.
source to share