nsarray数组越界_NSArray,NSMutableArray –目标C数组

nsarray数组越界

NSArray and NSMutableArray are the Objective C array objects. In this tutorial we’ll discuss NSArray at a basic level along with various functions available in the class. NSArray help us in creating static array in Objective C programming. Earlier we looked into NSNumber and NSString

NSArray和NSMutableArray是Objective C数组对象。 在本教程中,我们将在基本级别上讨论NSArray以及该类中可用的各种函数。 NSArray帮助我们在Objective C编程中创建静态数组。 之前我们研究了NSNumber和NSString

NSArray NSMutableArray (NSArray NSMutableArray)

NSArray is Objective-C’s general-purpose array type. It represents an ordered collection of objects. NSArray is immutable, so we cannot dynamically add or remove items. Its mutable counterpart, NSMutableArray, will be discussed in the second part of this tutorial.

NSArray是Objective-C的通用数组类型。 它代表对象的有序集合。 NSArray是不可变的 ,因此我们无法动态添加或删除项目。 本教程的第二部分将讨论其可变的对等NSMutableArray

NSArray示例 (NSArray Example)

Immutable arrays can be defined as literals using the @[] syntax. Another way to create arrays is using arrayWithObjects: method. Both the approaches are shown below :

可以使用@ []语法将不可变数组定义为文字。 创建数组的另一种方法是使用arrayWithObjects:方法。 这两种方法如下所示:

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];
NSArray *cities = [NSArray arrayWithObjects:@"New Delhi",
                    @"London", @"Brisbane", @"Adelaide", nil];

NSLog(@"colors array displayed: %@", colors[0]);
NSLog(@"cities array displayed: %@", [cities objectAtIndex:0]);

objectAtIndex: is the standard way to access elements from an array.
count method is used to get the number of elements in an array.

objectAtIndex:是从数组访问元素的标准方法。
count方法用于获取数组中元素的数量。

枚举NSArray (Enumerating NSArray)

Enumerating over an array is possible using any of the following two types :

可以使用以下两种类型中的任何一种来枚举数组:

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];

for (NSString *item in colors) {
    NSLog(@"%@", item);
}
for (int i=0; i<[colors count]; i++) {
    NSLog(@"%d: %@", i, colors[i]);
}

比较目标C数组 (Comparing Objective C Arrays)

isEqualToArray: is used to compare two NSArrays. It returns a YES if both the arrays contain the same number of elements and every pair passes the isEqual: test.

isEqualToArray:用于比较两个NSArray。 如果两个数组包含相同数量的元素,并且每对都通过isEqual:测试,则返回YES。

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];
NSArray *same_colors = [NSArray arrayWithObjects:@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet",nil];

if ([colors isEqualToArray:same_colors]) {
    NSLog(@"Success! Both the arrays are same");
}

Note: To check if a given element exists in the NSArray, containsObject: method is used with the string literal passed as the parameter.

注意 :要检查NSArray中是否存在给定元素,可将containsObject:方法与作为参数传递的字符串文字一起使用。

To retrieve the index of an NSArray element, indexOfObject: method is used. This either returns the index of the first occurrence of the requested object or NSNotFound if it’s not in the array. An example is given in the snippet below. Try it out in XCode!

要检索NSArray元素的索引,请使用indexOfObject:方法。 这将返回所请求对象首次出现的索引,或者返回NSNotFound(如果不在数组中)。 下面的代码段提供了一个示例。 在XCode中尝试一下!

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];
// BOOL checking
if ([colors containsObject:@"Red"]) {
    NSLog(@"Red is an element of colors array");
}
// Index checking
NSUInteger index = [colors indexOfObject:@"Red"];
if (index == NSNotFound) {
    NSLog(@"Well that's not found");
} else {
    NSLog(@"Red is an element of colors array and is at index %ld", index);
}

结合NSArrays (Combining NSArrays)

Arrays can be combined via arrayByAddingObjectsFromArray:. This returns a new array containing all of the elements in the original array, along with the array passed as a parameter.

可以通过arrayByAddingObjectsFromArray:组合数组。 这将返回一个包含原始数组中所有元素的新数组,以及作为参数传递的数组。

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];
NSArray *moreColors = @[@"Indigo", @"Black", @"White", @"Grey"];

NSArray *allColors = [colors arrayByAddingObjectsFromArray:moreColors];
NSLog(@"%@", allColors);

字符串转换 (String Conversion)

The componentsJoinedByString: method concatenates each element of the array into a string, separated by the delimiters specified in the parameter. Following snippet implements the same.

componentsJoinedByString:方法将数组的每个元素连接成一个字符串,由参数中指定的分隔符分隔。 以下代码段实现相同。

NSArray *colors = @[@"Red", @"Yellow", @"Orange",
                         @"Green", @"Blue", @"Violet"];
NSLog(@"%@", [colors componentsJoinedByString:@", "]);

NSMutableArray (NSMutableArray)

The NSMutableArray class allows us to dynamically add or remove items from arbitrary locations in the collection. Following snippets shows an example to create Mutable Arrays:

NSMutableArray类允许我们从集合中的任意位置动态添加或删除项目。 以下片段显示了创建可变数组的示例:

NSMutableArray *sports = [NSMutableArray arrayWithObjects:
                              @"Cricket", @"Football",
                              @"Hockey", @"Table Tennis", nil];

We can create empty mutable arrays using the array or arrayWithCapacity: class methods. To create a mutable array from an immutable array we can pass the immutable array to the arrayWithArray: class method.

我们可以使用arrayarrayWithCapacity:类方法创建空的可变数组。 要从不可变数组创建可变数组,我们可以将不可arrayWithArray:组传递给arrayWithArray:类方法。

从NSArray添加和删除对象 (Adding and Removing Objects from NSArray)

The two basic methods for manipulating the contents of an array are the addObject: and removeLastObject methods which are self explanatory. The snippet below implements the same.

操纵数组内容的两个基本方法是addObject:removeLastObject方法,这些方法很addObject:解释。 下面的代码段实现了相同的功能。

NSMutableArray *sports = [NSMutableArray arrayWithObjects:
                              @"Cricket", @"Football",
                              @"Hockey", @"Table Tennis", nil];
[sports addObject:@"Basketball"];
NSLog(@"%@", sports);       // Basketball added to end
[sports removeLastObject];
NSLog(@"%@", sports);       // Basketball removed from end

其他收藏类 (Other Collection Classes)

NSSet and NSDictionaries are the other two well known collection classes of the Foundation Framework.

NSSet和NSDictionaries是Foundation Framework的另外两个众所周知的集合类。

  • NSSet class represents a static, unordered collection of distinct objects. Sets are primarily optimised for membership checking

    NSSet类表示不同对象的静态无序集合。 集主要针对成员资格检查进行了优化
  • NSDictionary class represents an unordered collection of objects. However, they associate each value with a key, which acts like a label for the value. This is useful for modelling relationships between pairs of objects

    NSDictionary类表示对象的无序集合。 但是,它们将每个值与一个键相关联,键的作用类似于该值的标签。 这对建模对象对之间的关​​系很有用

This brings an end to NSArray example tutorial. These Objective C Tutorials covered would be useful in the iOS Tutorials.

这样就结束了NSArray示例教程。 这些涵盖的Objective C教程在iOS教程中将非常有用。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/10182/nsarray-nsmutablearray-objective-c-array

nsarray数组越界

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值