我想通过我的NSArray搜索某个字符串.
例:
NSArray有对象:"狗","猫","胖狗","东西","另一件事","这里是另一回事"
我想搜索单词"another"并将结果放入一个数组,并将另一个非结果放入另一个可以进一步过滤的数组中.
我需要显示6个视图,每个视图应显示20个项目(UIButtons).我有一个大的NSArray,其中包含所有6个视图的项目.
例如,视图1应为项目0-19,视图2应为项目20-39.
我如何从阵列中提取相关范围?也许使用长度为20的NSRange,但是每个视图都需要更改起始位置...理想情况下没有switch语句:)
谢谢
是否有一个转换的好方法vector<int32_t>到NSArray的NSNumber或循环,并增加了NSMutableArray相当多的唯一途径?
请考虑以下声明:
for (NSString *string in anArray) {
NSLog(@"%@", string);
}
Run Code Online (Sandbox Code Playgroud)
如何string在anArray不使用传统for循环的情况下获取in 的索引,而不检查string每个对象的值anArray?
我想在NSARRAY的第一个索引处添加@"ALL ITEMS"对象.
最初,Array有10个对象.添加后,该数组应包含11个对象.
我有一个按字母顺序排序的名称列表,现在我想在表格视图中显示这些名称.我正在努力为每个字母分组这些名字.
我的代码看起来像这样:
let sections:Array<AnyObject> = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var usernames = [String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cellID = "cell"
let cell: UITableViewCell = self.tv.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
cell.textLabel?.text = usernames[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return usernames.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 26
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject]!{
return self.sections
}
func tableView(tableView: UITableView,
sectionForSectionIndexTitle title: String,
atIndex index: Int) -> Int{
return index
}
func tableView(tableView: UITableView,
titleForHeaderInSection …Run Code Online (Sandbox Code Playgroud) 这感觉就像是一个愚蠢的问题,但我怎么能找到一个字符串NSArray?
我试过用
[array indexOfObjectIdenticalTo:myString]
Run Code Online (Sandbox Code Playgroud)
但这需要刺痛具有相同的地址.
有没有人有关于如何做到这一点的任何提示?
我想知道是否可以使用NSCoder方法:
- (void)encodeObject:(id)objv forKey:(NSString *)key
Run Code Online (Sandbox Code Playgroud)
编码NSArray或NSDictionary的实例.如果不是,你怎么去编码呢?我正在尝试使用NSKeyedArchived/NSKeyedUnarchiver使用GameKit在手机之间发送数据.我试过它,似乎无法检索我存储在我的数组中的字符串.我创建的数据包类与数据包类型整数一起出现,但不是数组中的数据.
提前致谢!
测试NSArray是否包含某种类型的对象的最佳方法是什么?containsObject:似乎要测试平等,而我正在寻找isKindOfClass:平等检查.
我需要NSNumber从另一个生成一个排序的s 数组NSArray.我想用这个sortedArrayUsingComparator:方法.我在Apple文档中找到了这个例子:
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
if ([obj1 integerValue] > [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
Run Code Online (Sandbox Code Playgroud)
这里我只是在块中提供比较器,这很好.排序本身 - 隐藏在我身上的一点,它使用了这个块 - 似乎是在提升.
我想知道我是否可以在此块外部请求不同的排序顺序,或者我是否只需要翻转<和>(或NSOrderedXXX)s?