2014-11-26 2 views
0

나의 현재 SWIFT 코드Swift에서 변수를 인스턴스화하는 방법은 무엇입니까?

var itemToEdit = ChecklistItem 

(그때의 viewDidLoad 방법에이 VAR를 사용합니다)

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let item = itemToEdit { 
     title = "Edit Item" 
     textField.text = item.text 
    } 

} 

오류 얻고있다 : 첫 번째 줄에 "예상 멤버 이름 또는 생성자 호출 후 유형 이름을" . ChecklistItem 개체 뒤에 "()"을 사용하고 개체 뒤에 ".self"를 쓰려고 시도한 다음 "if item if itemToEdit"및 "textField.text = item.text"행에 2 개의 오류가 발생했습니다. 미리 감사드립니다.

답변

3

"CheckListItem"이 유형 이름 인 경우 초기화 프로그램을 호출하여 인스턴스를 만들어야합니다.

var obj = CheckListItem() // Initializer with no arguments. 

Swift에는 일반 유형과 선택적 유형 인 두 가지 변수 유형이 있습니다.이 유형은 0 일 수 있습니다.

var optionalValue: Int? = nil //This is Optional type. (Add '?' to end of Type name) 
var normalValue: Int = nil //Error. Normal Int can not be nil. 

항목이 전무 여부 확인 "= itemToEdit을 {항목을 할 경우"여부, itemToEdit은 선택 유형이어야하기 때문에

.

var itemToEdit: CheckListItem? = CheckListItem() //Declare as Optional type. 
+0

대단히 감사합니다. – ArtStyle

+0

도와 드리겠습니다. :) –

관련 문제