- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我认为新符号是这样工作的:
someArray[5] 变成
someArray[5] 实际上会变成 [someArray objectAtIndexedSubscript:5]
但是,在 NSArray.h 和 NSOrderedSet.h 中我看到了这个:
- (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
因此,objectAtIndexedSubscript 仅适用于 IOS6。
我尝试制作这个简单的代码:
NSArray * someArray =@[@"hello",@"World",@"World"];
NSOrderedSet * someOS = [NSOrderedSet orderedSetWithArray:someArray];
PO(someArray);
PO(someOS);
PO(someArray[0]);
PO(someOS[0]); //Exception thrown here
代码在 someOS[0] 处中断
-[__NSOrderedSetI objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x8b1fac0
在NSArray和NSOrderedSet中,都有一个文本NS_AVAILABLE(10_8, 6_0);
然而它并没有在 NSArray 上中断,而是在 NSOrderedSet 上中断。为什么?
奖励:我如何使它也适用于 NSOrderedSet 和类别(需要检查它是否尚未定义)
最佳答案
我有更好的答案!
此代码将为不支持 -objectAtIndexedSubscript:
的 iOS 版本动态修补 NSOrderedSet
。
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
id PatchedObjectAtIndexedSubscript(id self_, SEL cmd_, NSUInteger index)
{
return [self_ objectAtIndex:index];
}
void PatchedSetObjectAtIndexedSubscript(id self_, SEL cmd_, id object, NSUInteger index)
{
return [self_ replaceObjectAtIndex:index withObject:object];
}
void SIPatchNSOrderedSet()
{
char types[6];
if (!class_getInstanceMethod([NSOrderedSet class], @selector(objectAtIndexedSubscript:))) {
sprintf(types, "@@:%s", @encode(NSUInteger));
class_addMethod([NSOrderedSet class],
@selector(objectAtIndexedSubscript:),
(IMP)PatchedObjectAtIndexedSubscript,
types);
}
if (!class_getInstanceMethod([NSMutableOrderedSet class], @selector(setObject:atIndexedSubscript:))) {
sprintf(types, "v@:@%s", @encode(NSUInteger));
class_addMethod([NSMutableOrderedSet class],
@selector(setObject:atIndexedSubscript:),
(IMP)PatchedSetObjectAtIndexedSubscript,
types);
}
}
在您的应用程序开始时(-application:didFinishLaunchingWithOptions:
可能)调用 SIPatchNSOrderedSet()
。
关于objective-c - someArray[5] 为什么以及如何在 objective-c 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391251/
sorted_copy 函数的以下两个实现之间是否存在实质性差异(注意:只有最后一行不同): /* version 1 */ function sorted_copy ( array ) {
所以我了解到 Ember 可以对数组和其中的元素进行计算。这里有两个选项。 someArray.[] 和someArray.@each 如果说我更改了数组中的一个元素,并且有一个计算属性依赖于它。我应
我一直在尝试使用 JSON 数据处理一些项目,但我遇到了一个问题,当我声明我的数组 someArray: [Int]? 时,打印的数据都是 nils。然而,当我更改为 someArray:[Int]
#include int main() { int a[]={11,12,13},*p; printf("%x\n",a); for(int i=0;i<3;i++)
我有一个简单的 C 问题。这句话是什么意思? if (!someArray[i]) 我知道运营商!表示不。但我无法理解它。谢谢!! 最佳答案 if (!someArray[i]) 表示如果 some
.contains 是否可以有多个参数?我正在搜索数组以确保每个字符串包含多个字符之一。我在网上搜寻过,但没有发现任何有用的东西。 for(String s : fileContent
我尝试用谷歌搜索这个,但谷歌不能很好地处理“--n”。我在教授的代码中看到了这一点: f[--n]; f[n++]; 其中 f 是 double 值数组。 我的猜测是它在减少(或增加)到 n 之前返回
我是 reading about Deferreds and Promises并不断遇到 $.when.apply($, someArray)。我有点不清楚这到底是做什么的,正在寻找 one line
var array = [3,9,23,76,1,54,21,12,0,9,2]; var shuffled = array.sort(function() {return 0.5 - Math.ra
现在尝试创建一些代码,从 CSV 文件中获取一个字符串并将其与某些条件进行比较。如果此字符串通过条件,则将其分为 4 部分 - 将每部分放入数组中,然后从 TextBox 中获取一些新值并进行更改。
现在尝试创建一些代码,从 CSV 文件中获取一个字符串并将其与一些标准进行比较。如果此字符串通过标准,则将其分成 4 部分 - 将每个部分放入数组中,然后从 TextBox 中获取一些新值并进行更改。
有谁知道为什么在带有针对 ES6 的 compilerOptions 的 TypeScript 中,我使用以下语法来迭代数组?它适用于 Edge 和 Chrome,但不适用于 IE11。它提示了一个;
我只是在用 AngularJS 做一些事情。我注意到通过使用 if 条件的附加样式,我总是得到错误的结果。为什么这样?我错过了什么吗?我试图用谷歌搜索它,但没有找到任何东西。 var myApp =
这个有效: pressure := &dataDump[845] CurrentPressure := *pressure 但是有没有办法改变第一行,使 pressure 成为 dataDump[84
我无法将 3 个数组发送到 SQL 语句中,它会引发异常 select t.ttt, t.created_date, concat_ws('-',lp.mmm, lp.ccc, lp.eee ) fr
我认为新符号是这样工作的: someArray[5] 变成 someArray[5] 实际上会变成 [someArray objectAtIndexedSubscript:5] 但是,在 NSArra
相关 fiddle :https://jsfiddle.net/tqf4zea7/1/ 我在 Angular Controller 中使用 $q。为了测试某些场景,我在作用域上创建了一个数组以将消息推
我是一名优秀的程序员,十分优秀!