obj-c 부분 대상 빠른 값 부여 및 값 가져오기

6465 단어 대상
NSNumber:
NSNumber *number = @1234; 
 
기존 방식:
NSArray *physicsValues = [NSArrayarrayWithObjects:



                  [NSNumbernumberWithDouble:6.02214129e23],



                  [NSNumbernumberWithDouble:1.3806503e-23],



                  [NSNumbernumberWithDouble:6.626068e-34],



                  [NSNumbernumberWithDouble:1.097373e-7],



                  nil];

 
우리는 다음과 같은 방법으로 대체할 수 있다.
NSArray *numberArray = [NSArray arrayWithObjects: 



                                @6.02129e23        



                                @1.380, 



                                @6.62,



                                @1.097373e-7, 



                                nil]; 

하지만 위의 코드는 더욱 간단할 수 있다.
 
 1  NSArray *numberArray = @[@6.02129e23,  

 2                           @1.380, 

 3                           @6.62,

 4                           @1.097373e-7]; 

 5 

 6 

 7  NSArray *stringArray = @[ @"good",

 8                            @"see"  

 9                            @"xCode", 

10                            @"what"];

 
물론 사전에도 유사한 방법이 있습니다. 수조는 @[]이고 사전은 @{}입니다
오래된 방법을 살펴보자.
1 nameDict = [NSDictionary dictionaryWithObjectsAndKeys:

2 

3                   physic, @"Physic",

4 

5                   newStrings, @"string",

6 

7                   number, @"Numbers",

8                   nil]; 

새로운 방법:
1 nameDict = @{

2                 @"Physic" : physic,

3 

4                 @"string" : newStrings,

5 

6                 @"Numbers" : number

7 

8             };

 
배열에 대해 우리는 값을 찾으려고 하는데 일반적으로 [array objectAtindex:0]를 사용한다.
하지만 이제 더 편리하고 빠른 방법이 생겼다. 바로 아래 표지의 array[0]를 직접 사용하는 것이다. 효과는 앞의 문장과 같다.
데이터를 바꾸는 것과 같이 아래로 표시할 수 있습니다
 1 //

 2 

 3 [valuesArray replaceObjectAtIndex:controller.indexInSection withObject:controller.value];

 4 

 5 [valuesDictionary setObject:valuesArray forKey:controller.sectionName]; 

 6 

 7 //

 8 

 9 valuesArray[controller.indexInSection] = controller.value; 

10 

11 valuesDictionary[controller.sectionName] = valuesArray; 

 
상기 특성을 사용하면 우리는 OS 버전에 관심을 가질 필요가 없다. 이것은 컴파일러의 일이기 때문에 우리는 얼마든지 안심하고 할 수 있다.

좋은 웹페이지 즐겨찾기