Reputation: 4046
I've got some swift code that looks like this:
class GeoRssItem
{
var title = ""
var description = ""
}
In another class I declare a variable of this as:
var currentGeoRssItem : GeoRssItem? // The current item that we're processing
I allocate this member variable as:
self.currentGeoRssItem = GeoRssItem();
then when I try to assign a property on the self.currentGeoRssItem Xcode auto completes to this:
self.currentGeoRssItem.?.description = "test"
Which then fails with a build error as so:
"Expected member name following '.'"
How do I set this property? I've read the docs but they aren't very helpful.
Upvotes: 1
Views: 1043
Reputation: 45408
If you want to assert that the value is non-nil, you can do:
self.currentGeoRssItem!.description = "test"
If you want the statement to be a no-op if the variable is nil, then
self.currentGeoRssItem?.description = "test"
Upvotes: 0
Reputation: 1093
The question mark is in the wrong place. Should be:
self.currentGeoRssItem?.description = "test"
However you might get: "Cannot assign to the result of this expression"
.
In that case you need to check for nil like this:
if let geoRssItem = self.currentGeoRssItem? {
geoRssItem.description = "test"
}
Upvotes: 1