2014-10-18 2 views
-1

객체 C 스타일 나는사전 어떻게 사전 배열을 만들 수 있습니까?

class ViewController: UIViewController { 
    var datalist = [Dictionary]() //Compile Error -> "Missing argument for parameter #1 in call" 
            // I can't understand error. What does it mean? 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     let dict1 = ["age":"32","name":"andy"] 
     let dict2 = ["age":"34","name":"smith"] 
     let dict3 = ["age":"27","name":"jonathan"] 

     datalist = [dict1,dict2,dict3] //Compile Error -> 'ViewController' does not have a member named 'datalist' 
    } 
} 

하지만이 오류를

내가하지 다음과 같이

그래서 빠른 스타일을 입력 스위프트에 동일한 코드를 만들고 싶어

@interface TestViewController:UIViewController{ 
    NSArray *dataList; 
} 
@end 

@implementation TestViewController 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSDictionary *dict1 =[[NSDictionary alloc] initWithObjectsAndKeys:@"32",@"age",@"andy",@"name", nil]; 
    NSDictionary *dict1 =[[NSDictionary alloc] initWithObjectsAndKeys:@"34",@"age",@"smith",@"name", nil]; 
    NSDictionary *dict1 =[[NSDictionary alloc] initWithObjectsAndKeys:@"27",@"age",@"jonathan",@"name", nil]; 

    dataList = [[NSArray alloc]initWithObjects:dict1,dict2,dict3,nil]; 
} 
@end 

왜 이슈 오류인지 알 수 있습니다

도와 주시겠습니까? 신속한에서

답변

3

배열은 그냥 예를 들어, (또는 값 없음) []와 동일한 배열을 설정하여 하나를 생성하는 매개 변수를 사용하지 않습니다

var array : [Int] = [] 
var array2 = [1,2,3] 

정수의 배열을 만들 것입니다. 또한

, 예를 들어 키와 값의 유형을 알 수있는 신속 필요에 사전 :

Dictionary<String, String> 

그러나처럼 보인다 사전 만들기위한 속기도있다 :

[String:String] 

은 (String이 키와 값의 유형이라고 가정).

var datalist : [[String:String]] = [] 

그럼 당신은 당신이 그것을 사용하거나 그것을 [String:String] 객체를 추가하여하는 방법으로 DataList에 객체를 사용할 수 있습니다

은 그래서 예를 들어, 당신은 당신의 DataList에 개체로 선언 할 재 작성 할 수 있습니다.

+0

정말 고맙습니다. – user1272854

관련 문제