Author Topic: Chapter 4 Optional chaining  (Read 7098 times)

shx

  • Newbie
  • *
  • Posts: 12
    • View Profile
Chapter 4 Optional chaining
« on: January 25, 2015, 03:31:44 PM »
Kevin, could you explain
Quote
“Although you can retrieve a property value through optional chaining, you can’t set its value this way
It seems that I'm able to set the locationDescription with the following code
Code: [Select]
memberEntity.location?.locationDescription = "hello"
when I run
Code: [Select]
var locationDesc = memberEntity.location?.locationDescription
I could see it worked.

Steven

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Chapter 4 Optional chaining
« Reply #1 on: January 26, 2015, 10:06:26 AM »
Steven,

Good catch...in the initial release of Swift, you couldn't set a property through optional chaining, but now you can. The same is also true of subscripts. You can now set a subscript value using optional chaining. I'll fix this in the next book release!

All the best!
Kevin
« Last Edit: January 26, 2015, 10:15:49 AM by kjmcneish »

BillF

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Chapter 4 Optional chaining
« Reply #2 on: April 24, 2015, 09:20:56 AM »
I'm on the same chapter, I'll paste my code below, (sorry for the comments, before I read what you say it does I've been going through and commenting what I think the code may do)

This is from memberEntity.swift

//
//  MemberEntity.swift
//  OptionalChainingDemo
//
//  Created by Kevin McNeish on 9/8/14.
//  Copyright (c) 2014 Oak Leaf Enterprises, Inc. All rights reserved.
//

//imports the Cocoa Touch framework named foundation which contains low-level core classes such as collections, strings, as well as date and time managment
import Foundation

//creates a class named MemberEntity
class MemberEntity {
   
    //creates an optional named location equal to a varaible called LocationEntity I think
    var location : LocationEntity?
   
    //creates a variable named firstName that is of type string and is empty
    var firstName : String = ""
   
    //creates a variable named lastName that is of type string and is empty
    var lastName : String = ""
   
    //Initializes the class (I think)
    init() {
       
    }
}

This is from OptionalChainingDemoTests.swift

//
//  OptionalChainingDemoTests.swift
//  OptionalChainingDemoTests
//
//  Created by Kevin McNeish on 9/8/14.
//  Copyright (c) 2014 Oak Leaf Enterprises, Inc. All rights reserved.
//

//imports the Cocoa Touch Framework called UIKit which contains UI controls from apple for developers to use in thier apps
import UIKit

//imports the Cocoa Touch Framewokr called XCTest which does something that I don't know.
import XCTest

class OptionalChainingDemoTests: XCTestCase {
   
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
   
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testOptionalChaining() {
        var memberEntity = MemberEntity()
        var nameOfLocation: String? = MemberEntity.location?.LocationName
       
        XCTAssertNil(nameOfLocation)
    }
}


I'm getting the error Coding/SamplesSwift/OptionalChainingDemo/OptionalChainingDemoTests/OptionalChainingDemoTests.swift:29:39: 'MemberEntity.Type' does not have a member named 'location'

But I look and I can see that MemberEntity.swift has a variable named location.

Can you tell me what I'm doing wrong?

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Chapter 4 Optional chaining
« Reply #3 on: April 24, 2015, 01:31:42 PM »
On the following line of code, the "M" in MemberEntity needs to be lower case:

Code: [Select]
var nameOfLocation: String? = MemberEntity.location?.LocationName
When you use an upper case "M" you are referencing the MemberEntity class which doesn't have a class property named location. When you use a lower case "m" you are referencing the memberEntity object you created in the previous line of code:

Code: [Select]
var memberEntity = MemberEntity()
All the best!
Kevin

BillF

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Chapter 4 Optional chaining
« Reply #4 on: April 24, 2015, 02:37:23 PM »
Thank you!