2014-12-13 2 views
0

대물 렌즈 c를 신속하게 번역하려고하면 크게 좌절감을 느낍니다. 나는 객관적인 c를 위해 작동하는 다음 코드를 가지고있다.플리스 스트랩 풀기 및 대상 찾기

NSMutableArray *path = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sequence List" ofType:@"plist"]]; 

//Shuffle the array of questions 
numberSequenceList = [self shuffleArray:path]; 

currentQuestion = currentQuestion + 1; 

if (Round==1) { 
    //Take first object in shuffled array as the first question 
    NSMutableArray *firstQuestion = [[NSMutableArray alloc] initWithArray:[numberSequenceList objectAtIndex:0]]; 

    //Find question and populate text view 
    NSString *string = [firstQuestion objectAtIndex:0]; 

    self.lblNumber.text = string; 

    //Find and store the answer 
    NSString *findAnswer = [firstQuestion objectAtIndex:1]; 

    Answer = [findAnswer intValue]; 
} 

하지만이 작업을 신속하게 처리 할 수는 없습니다. 나는

var path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist") 

을 사용하여 plist의 내용을 추출 할 수 있습니다. 그러나 objectAtIndex와 동일한 기능이 있음을 알 수 없습니다. 다음을 시도하면 "문자열에 subscript라는 멤버가 없습니다"라는 오류 메시지가 표시됩니다. 이는 경로를 해제해야한다는 것을 의미합니다.

let firstQuestion = path[0] 

답변

0

NSBundle.mainBundle().pathForResource과 같이 호출하는 메서드는 실패 할 수 있으므로 optionals를 반환합니다. Objective-C에서이 오류는 nil으로 표시되지만 Swift는 옵션을 사용합니다. 그래서 예에서

:

var path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist") 

path 유형 Optional<String> (또는 String?)이며 형태가 아닌 String의. Optional<String>에는 첨자 메소드가 없습니다 (즉, [ ]을 지원하지 않습니다). 이 옵션에 값이 있는지 확인해야 내에서 문자열을 사용하려면 (즉, pathForResource에 대한 호출이 성공) :

// the if let syntax checks if the optional contains a valid 
if let path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist”) { 
    // use path, which will now be of type String 
} 
else { 
    // deal with pathForResource failing 
} 

당신은 the Swift book의 도입에 선택적 항목에 대한 자세한 내용을보실 수 있습니다.

+0

안녕하세요. 설명해 주셔서 감사합니다. 그러나 if 함수를 사용하려고하면 'Subscript is unavailable : int로 문자열을 첨자화할 수 없습니다'라는 메시지가 나타납니다. 어떻게 문자열의 첫 번째 객체를 사용할 수 있습니까? – user1759949

+0

'first (mystring)'(다시 문자열을 비울 수 있기 때문에 선택 사항을 반환). 또는 'mystring [mystring.startIndex]'- 문자열이 정수 색인 가능하지 않은 이유는 임의의 문자가 다른 바이트 길이 일 수 있기 때문입니다. –

0

Objective-C에서 전체 첫 줄을 번역하지 않았습니다. 파일의 내용에서 배열을 만드는 NSMutableArray에 대한 호출이 누락되었습니다. 원래의 코드는 실제로 질문 할 때 path 파일의 내용을 호출하기 때문에 혼란 스럽습니다. 사용해보기 :

if let path = NSBundle.mainBundle().pathForResource("Sequence List", ofType: "plist") { 
    let questions = NSMutableArray(contentsOfFile: path) 

    //Shuffle the array of questions 
    numberSequenceList = self.shuffleArray(questions) 

    currentQuestion = currentQuestion + 1 

    if Round == 1 { 
     //Take first object in shuffled array as the first question 
     let firstQuestion = numberSequenceList[0] as NSArray 

     //Find question and populate text view 
     let string = firstQuestion[0] as NSString 

     self.lblNumber.text = string 

     //Find and store the answer 
     let findAnswer = firstQuestion[1] as NSString 

     Answer = findAnswer.intValue 
    } 
} 
관련 문제