我有一个带有NSIndexPaths的NSArray
NSArray *array = [self.tableView indexPathsForSelectedRows];
for (int i = 0; i < [array count]; i++) {
NSLog(@"%@",[array objectAtIndex:i]);
}
Run Code Online (Sandbox Code Playgroud)
NSLog返回:
<NSIndexPath 0x5772fc0> 2 indexes [0, 0]
<NSIndexPath 0x577cfa0> 2 indexes [0, 1]
<NSIndexPath 0x577dfa0> 2 indexes [0, 2]
Run Code Online (Sandbox Code Playgroud)
我试图从indexPath获取第二个值只是一个普通的NSInteger
我有一个NSMutableArray
@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;
@end
Run Code Online (Sandbox Code Playgroud)
我正在尝试添加我的数组NSInteger对象:
@synthesize reponses;
NSInteger val2 = [indexPath row];
[reponses addObject:[NSNumber numberWithInteger:val2]];
NSLog(@"the array is %@ and the value is %i",reponses, val2);
Run Code Online (Sandbox Code Playgroud)
如果没有将对象添加到数组中它将无法工作,这是控制台显示的内容:
the array is (null) and the value is 2
Run Code Online (Sandbox Code Playgroud) 我试图将NSInteger设置为NULL.虽然编译器没有给出任何错误,但我不确定这是否是正确的方法.你能在ios中将NSInteger设置为NULL吗?或者是出于某种原因禁止..?或者我应该把它设置为Nil ..?哪个更好的做法..?
谢谢
这是对这个帖子的一些跟进,但是有一个不同的问题,所以我觉得我应该在另一个帖子中提问.
我在内存中有四个连续字节,我从一个文件读入.我想将它们存储为一个位数组(它们的实际int值直到后来都无关紧要).当我打印出我的int中的内容时,我注意到它似乎以相反的顺序存储(小端).
有没有人有一个很好的方法来反转字节的顺序.然后反转,挑出跨越两个字节的连续位并转换回int?
unsigned char data[] = { 0x00, 0x02, 0x45, 0x28 };
NSInteger intData = *((NSInteger *)data);
NSLog(@"data:%08x", intData); // data:28450200
Run Code Online (Sandbox Code Playgroud) NSUInteger index = [self.objects indexOfObject:obj];
if (index == NSNotFound) {
// Success! Note: NSNotFound internally uses NSIntegerMax
}
if (index == NSUIntegerMax) {
// Fails!
}
Run Code Online (Sandbox Code Playgroud)
为什么?我想由于indexOfObject而得到一个无符号值.所以,当然,我假设如果找不到该对象,它将返回NSUIntegerMax而不是NSIntegerMax.这是一个错误,还是对这种行为有合理的解释.
我正在尝试排序一个数组NSObjects(一个对象是一个类).对象类有几个变量,我想用它来排序数组中的对象,我用来排序的变量是类型的,NSString或者NSInteger我有信心我正在排序 NSStrings但是对于这个问题,我希望得到帮助分选NSInteger.
这是我用来对对象数组进行排序的方法,它接收一个NSMutableArray对象
- (NSMutableArray *)startSortingTheArray:(NSMutableArray *)unsortedArray
{
[unsortedArray sortUsingComparator:^ NSComparisonResult(SearchResultItem *d1, SearchResultItem *d2) {
NSInteger DoorID1 = d1.doorID;
NSInteger DoorID2 = d2.doorID;
NSComparisonResult result = [DoorID1 localizedCompare:DoorID2]; // Not sure how to sort these two integers
if (result == NSOrderedSame) {
NSString *handel1 = d1.handel;
NSString *handel2 = d2.handel;
result = [handel1 localizedCompare:handel2];
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何比较NSInteger,我已经读过我可以减去它自己的每个数字..如果数字是0那么它们是相等的还是+1/-1等它们不是......但不确定那是不是接近这个的最好方法.
任何帮助,将不胜感激.
我在XCode中为我的iOS项目打开了“签名比较”(又名-Wsign-compare)警告(令人惊讶的是,默认情况下它处于关闭状态)。之后,出现了许多这样的警告:
/Users/michalciuba/projects/GlobeMobile/Classes/ACMailController.m:86:19: Comparison of integers of different signs: 'NSInteger' (aka 'long') and 'NSUInteger' (aka 'unsigned long')
Run Code Online (Sandbox Code Playgroud)
它们通常通过比较造成row的财产NSIndexPath是NSInteger由“计数”的方法的返回值NSArray,如下所示:
if(indexPath.row < [self.myDataArray count])
Run Code Online (Sandbox Code Playgroud)
可以通过强制转换来简单地解决警告:
if(indexPath.row < (NSInteger)[self.myDataArray count])
Run Code Online (Sandbox Code Playgroud)
但是,如果要比较每个地方的值,则必须这样做。他们正在数十个地方进行比较。我想知道是否有更好,更聪明的方法来解决这个问题?我不想关闭此警告,因为它可能有助于防止出现无符号整数下溢之类的问题。
我今天花了一些时间追逐两个错误,并最终使用相同的解决方案修复它们.
现在我已经找到了解决方案,我希望能够明白这一点.
我正在将Core Data(Integer 16/NSNumber)的属性与Integer(ABPropertyID和ABMultiValueIdentifier)进行比较.
这个错误就在这个比较中,奇怪的是,只有在我杀死应用程序(从后台托盘),重新打开它并运行包含比较的相同过程后才显示自己.无论如何...
这是重启后停止工作的原因:
if (myNumber.aProperty == [NSNUmber numberWithInt:anInteger]) { /* do stuff here */ }
这两个解决方案到目前为止完美运行:
if ([myNumber.aProperty integerValue] == anInteger) {/* do stuff here */ }
if ([myNumber.aProperty isEqualToNumber:[NSNumber numberWithInt:anInteger]]) { /* do stuff here */ }
对我来说,它们看起来都一模一样.我总是要么将NSNumber转换为integerValue,要么将整数转换为NSNumber.
有任何想法吗?
假设我有一个@property是一个NSMutablearray,它包含四个对象使用的分数.它们将初始化为零,然后在viewDidLoad和应用程序的整个操作期间进行更新.
出于某种原因,我无法理解需要做的事情,特别是在声明和初始化步骤中.
我相信这可以是私有财产.
@property (strong, nonatomic) NSMutableArray *scores;
@synthesize scores = _scores;
Run Code Online (Sandbox Code Playgroud)
然后在viewDidLoad我尝试这样的东西,但得到一个错误.我想,我只需要语法方面的帮助.或者我遗漏了一些非常基本的东西.
self.scores = [[NSMutableArray alloc] initWithObjects:@0,@0,@0,@0,nil];
Run Code Online (Sandbox Code Playgroud)
这是初始化它的合适方式吗?然后我如何将(NSNumber*)updateValue添加到第n个值?
编辑:我想我弄清楚了.
-(void)updateScoreForBase:(int)baseIndex byIncrement:(int)scoreAdjustmentAmount
{
int previousValue = [[self.scores objectAtIndex:baseIndex] intValue];
int updatedValue = previousValue + scoreAdjustmentAmount;
[_scores replaceObjectAtIndex:baseIndex withObject:[NSNumber numberWithInt:updatedValue]];
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
我需要进行编码和解码类型的财产NSUInteger有NSCoder.
使用方法NSCoder::encodeInteger:forKey:和NSCoder::decodeIntegerForKey:方法是否安全?
我想到的另一种方法是将无符号整数包装到NSNumber第一位.但这意味着更多的代码,我不喜欢它.
nsinteger ×10
objective-c ×9
ios ×4
nsuinteger ×3
cocoa-touch ×2
iphone ×2
arrays ×1
byte ×1
bytearray ×1
clang ×1
endianness ×1
nsarray ×1
nscoder ×1
nsindexpath ×1
nsnumber ×1
uitableview ×1