XCODE2011. 3. 12. 17:13

// 초기화 및 배열 넣기

NSMutableArray *arrayData;

arrayData = [NSMutableArray arrayWithCapacity:listCount];

NSString *strData = @"아이폰";

[arrayData addObject:[NSString stringWithFormat:@"%@",strData]];

// Dic 배열에 넣기

NSMutableDictionary *item = [[NSMutableDictionary alloc] init];

[item setObject:[arrToken objectAtIndex:0] forKey:@"date"];

[item setObject:[arrToken objectAtIndex:1] forKey:@"price"];

[arrayData addObject:[item copy]];

// Method

  • addObject:

    Adds the given object to the end of the array.

  • insertObject:atIndex:

    Inserts the given object at index, moving the object there previously, and all objects at higher indices, one along.

  • removeObject:

    Removes all instances of an object in the array

  • removeObjectAtIndex:

    Removes the object at the given index.

  • replaceObjectAtIndex:withObject:

    Replaces the object at index with the one supplied. Returns an error if the index is out of bounds.

  • sortUsingSelector:

    Sorts an array; see SortUsingSelector

  • removeObjectIdenticalTo:

    checks for object identity before removing an object; see NSMutableArrayRemoveObjectIdenticalTo for an example of when this is necessary

출처 : http://blog.naver.com/PostView.nhn?blogId=hana_815&logNo=60112995891
Posted by 오늘마감

댓글을 달아 주세요

아이폰어플개발정보2011. 3. 12. 17:12
- NSArray는 다른 객체 포인터들의 목록이다.

- 자주 사용되는 메소드
- (unsigned)count -> 현재 배열의 객체의 수를 리턴한다.
- (id)objectAtIndex:(unsigned)i -> i번째 위치한 객체를 리턴한다.
- (id)lastObject -> 현재 배열의 맨 마지막 객체를 리턴한다.
- (BOOL)containsObject:(id)anObject -> 현재의 배열에 anObject오브젝트를 포함하면 YES 아니면 NO
- (unsigned)indexOfObject:(id)anObject -> 리시버에서 anObject가 있는지 가장 낮은 색인부터 검색하  여 배열의 색인 값을 리턴한다. 찾지 못하면 NSNotFound를 리턴한다.
NSArray *photoList =[NSArray arrayWithObjects:@"Photo 1.jpg", @"Photo 2.jpg", @"Photo 3.jpg", @"Photo 4.jpg",nil];

- NSMutableArray는 NSArray를 상속받았지만 객체를 삽입하고 삭제할 수 있도록 확장되었다.

- 자주 사용되는 메소드

             - (void)addObject:(id)anObject -> anObject를 리시버의 끝에 삽입한다. nil배열은 넣을 수 없다.

 - (void)addObjectsFromArray:(NSArray *)otherArray -> 매개체의 배열에 담긴 객체들을 넣는다.

 - (void)insertObject:(id)anObject atIndex:(unsigned)index -> index의 위치에 객체를 삽입한다.

             - (void)removeAllObjects -> 리시버에 포함된 모든객체를 제거한다.
             - (void)removeObject:(id)anObject -> 리시버 배열 안의 anObject를 삭제한다.
             - (void)removeObjectAtIndex;(unsigned)index -> index의 객체를 제거한다.


출처 : http://blog.naver.com/PostView.nhn?blogId=yesusok&logNo=50100674668
Posted by 오늘마감

댓글을 달아 주세요

오브젝트C2010. 10. 12. 13:04
[objective-c] Objective-C Tutorial: NSArray [영어주의]

Objective-C Tutorial: NSArray

Here at iCodeblog, we have been showing you guys how to create many different types of applications from the ground up.  Well, today I decided to do something different and get down to some of the nitty gritty of a structure we rely heavily on in objective-C.

The NSArray is a huge workhorse that we use quite frequently without even thinking about it.  The NSArray class isn’t just your ordinary array.  Not only does it provide random access, but it also dynamically re-sizes when you add new objects to it and has many methods to make our lives easier.  While I won’t go over every method in NSArray (there are quite a few), I will discuss some of the more important ones that are most commonly used.  Let’s take a closer look at this class.

Factory Methods

Factory methods are static methods that build new instances of NSArrays from given parameters and return them.  The table below details on all of the factory methods for the NSArray class.

+ (id)array Creates and returns an empty array
+ (id)arrayWithArray:(NSArray *)anArray Creates and returns an array containing the objects in another given array.
+ (id)arrayWithContentsOfFile:(NSString *)aPath Creates and returns an array containing the contents of the file specified by a given path. *The file must be of type .plist for this method to work
+ (id)arrayWithContentsOfURL:(NSURL *)aURL Similar to arrayWithContentsOfFile except it will load the .plist remotely from a given website. This would be a very simple way to get data from a web service.
+ (id)arrayWithObject:(id)anObject Creates and returns an array containing a given object. This will just be a 1 element array
+ (id)arrayWithObjects:(id)firstObj, … This method is used when you have multiple objects on hand and want easily insert them into an array. Make sure the last element you add is nil or this method won’t work.
+ (id)arrayWithObjects:(const id *)objects count:(NSUInteger)count Creates and returns an array that includes a given number of objects from a given C array.

Here is some example usage of building NSArrays with these factory methods…

// I am using strings, but you can add just about any object to an NSArray   // Creates an NSArray with one object NSArray * myArray = [NSArray arrayWithObject:@"foo"];   // Creates an NSArray with multiple objects. Don't forget to add nil as the last object NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];   // Creates an NSArray from another NSArray NSArray * myArray3 = [NSArray arrayWithArray:myArray2];   // This will create an NSArray from data on iCodeBlog. Go ahead and try it out, this file exists on our servers and contains valid data. NSArray * myArray4 = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://icodeblog.com/wp-content/uploads/2009/08/foo.plist"]];

You can also choose not to use factory methods and just use the normal NSArray initializers. They are pretty much the same as the factory methods only you do the allocation yourself. An example of this might be:

NSArray * foo = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];

Accessing The NSArray

Apple has provided us with many great methods for getting data out of an NSArray as well as information about it.  Here is a table of methods and their descriptions.

- (BOOL)containsObject:(id)anObject Returns true if a given object is found in the array, false otherwise
- (NSUInteger)count Returns the size of the array
- (id)lastObject Returns the last object in the array (the one with the highest index)
- (id)objectAtIndex:(NSUInteger)index Gives you random access to the array. Returns the object at a given index.

These are just a few of the accessor methods of NSArray and most likely the only ones you will need to use.

Searching The Array

If you are looking for the index of an object in an array, there is really only one method you need to use. That method is indexOfObject: . An example of usage might be:

NSString * f = @"foo"; NSString * b = @"bar"; NSString * z = @"baz"; NSArray * myArray2 = [NSArray arrayWithObjects:f,b,z,nil]; NSInteger idx = [myArray2 indexOfObject:b]; // This would return 1 (since NSArrays are 0 - indexed)

This is much cleaner code than looping over the entire array to find an object. It’s probably more efficient too as Apple is pretty clean in how they code things.

Sending Message To Objects In The Array

Ok, so this is pretty cool.  Say you have an array of objects that all need to do something.  For example, an array of bullets, and you want all of the bullets to move forward one pixel. Rather than looping over each bullet object in the bullets array and calling [bullet move], you can do it in one method call on the array.  This method is called – (void)makeObjectsPerformSelector:(SEL)aSelector and here is an example of usage.

// Lets pretend the bullet object has a method called move // and there is an array of 50 bullets   [bullets makeObjectsPerformSelector:@selector(move)];


And that’s it… The move method will now be called on every bullet object in the array with this one method call. Very clean and easy to use. Now, if the method you want to call takes an argument, there is the – (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)anObject method. This will allow you to pass an object to each method being called.

Sorting Arrays

Apple has provided us with some very slick ways to sort an NSArray. I will not go into too much detail here as I have a full tutorial planned dedicated to sorting arrays. For now, I will just show you how to sort an NSArray of NSStrings. The method we will be using is sortedArrayUsingSelector. Here is the example usage.

NSArray *sortedArray = [myArray2 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; // This will return a sorted array that looks like [@"bar",@"baz",@"foo"]

You can plug in any of the string compare functions there to compare the strings. If you are sorting an NSArray of custom objects (like users), you can overwrite the compare method for that object and pass compare in for the selector. Just make sure you remember the : at the end of the method.

Looping Through Arrays

So if you are reading this, I assume you have seen a for loop before. ex: for(int x=0; x < … This would most likely not be a good way to enumerate an NSArray as Apple has provided a much nice way for doing so. There are some situations where this method would be preferred (like when u need to calculate the indices or something). But in most cases, you will want to use the special for loop provided by Apple. If you have ever coded PHP, it is much like the foreach loop. Here is an example:

for(NSString * myStr in myArray2) { NSLog(myStr); }

This will loop over myArray2 and pring each element in that array. Very clean and efficient.

Saving Arrays For Later

There is an Array of ways to save data on the iPhone (Pun intended). One way is to simply dump the array to a file. This will write out the NSArray to a plist file on disk that can be loaded later using the arrayWithContentsOfFile method of NSArray. That method is rightfully named writeToFile. The example usage for it is as follows.

NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil]; [myArray2 writeToFile:filePath atomically:YES];

And there you have it! You are probably wondering what the atomically variable means. Me too… Just kidding. If YES, the file will be written to a temp location and then renamed to its final destination. This is put in place to guarantee that the file won’t be corrupted even if the system crashes (rename will be much faster than write). Why is this needed you ask? Consider this scenario.

You want to write a huge array to disk to save it for later. When your app starts it checks the disk to see if this file exists, if it does it loads an NSArray from it. If it doesn’t it creates a new NSArray. Say the last time the app ran, the system crashed while the file was being written. If atomically was set to NO, the original file would be corrupt and now the application would be loading corrupt data every time it starts from now on (most likely causing a crash). However, if you set atomically to YES, the temp file would get corrupted and the app would never see it. That way, the next time the app starts it will create a new fresh NSArray and all will be good.

Good rule of thumb, set atomically to YES.

That concludes our tutorial on NSArray. If you have any questions or comments, feel free to post them in the comments section or write me on twitter.

This entry was posted in iPhone ArticlesiPhone Programming Tutorialsiphone and tagged . Bookmark the permalinkPost a comment or leave a trackback: Trackback URL.
Posted by 오늘마감

댓글을 달아 주세요

오브젝트C2010. 10. 12. 04:46
[objective-c] Objective-C Tutorial: NSArray

Objective-C Tutorial: NSArray

Posted by 오늘마감

댓글을 달아 주세요

아이폰어플개발정보2010. 10. 5. 06:05
개발할때 유용한 NSArray 기본적인 소스



int main (int argc, char *argv[])
int i;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//매월의 이름을 담고 있는 배열
NSArray *monthNames = [NSArray arrayWithObjects :
@"January", @"February", @"March", @"April",
@"May", @"June", @"July", @"August", @"September",
@"October", @"November", @"December", nil];

//배열 안의 모든 원소를 나열한다.
NSLog (@"Month Name");
NSLog (@"==== ====");

for (i = 0;; i < 12; ++i)
NSLog (@"%2i   %@", i + 1, [monthNames objectAtIndex : i]);

[pool drain];
return 0;


출처 : http://blog.naver.com/PostList.nhn?blogId=hana_815¤tPage=58
Posted by 오늘마감

댓글을 달아 주세요