我有一个合成的NSMutableArray - theResultArray.我想在特定索引(0-49)处插入NSNumber或NSInteger对象.出于某种原因,我永远无法在我的数组中获得任何值.每个索引返回nil或0.
NSInteger timeNum = time;
[theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex];
NSLog(@"The right index is :%i", rightIndex);
NSLog(@"The attempted insert time :%i", time);
NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
NSLog(@"The result of time insert is:%i", [testNum intValue]);
Run Code Online (Sandbox Code Playgroud)
我在viewDidLoad中alloc-init theResultsArray.时间是整数.我一直在尝试上面代码的不同组合无济于事.
控制台输出:
StateOutlineFlashCards[20389:20b] The right index is :20
StateOutlineFlashCards[20389:20b] The attempted insert time :8
StateOutlineFlashCards[20389:20b] The result of time insert is:0
Run Code Online (Sandbox Code Playgroud) 我遇到了一段代码问题.我正在尝试使用addObject方法将一个CLLocationCoordinate2D实例添加到NSMutable数组,但每当执行该行时,我的应用程序崩溃.这段代码有什么明显的错误吗?
崩溃就在这条线上:
[points addObject:(id)new_coordinate];
Run Code Online (Sandbox Code Playgroud)
Polygon.m:
#import "Polygon.h"
@implementation Polygon
@synthesize points;
- (id)init {
self = [super init];
if(self) {
points = [[NSMutableArray alloc] init];
}
return self;
}
-(void)addPointLatitude:(double)latitude Longitude:(double)longitude {
NSLog(@"Adding Coordinate: [%f, %f] %d", latitude, longitude, [points count]);
CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
new_coordinate->latitude = latitude;
new_coordinate->longitude = longitude;
[points addObject:(id)new_coordinate];
NSLog(@"%d", [points count]);
}
-(bool)pointInPolygon:(CLLocationCoordinate2D*) p {
return true;
}
-(CLLocationCoordinate2D*) getNEBounds {
...
}
-(CLLocationCoordinate2D*) getSWBounds {
...
}
-(void) dealloc {
for(int count = 0; …Run Code Online (Sandbox Code Playgroud) 我有一个包含数组对象的数组.一组数组.当我将描述方法应用于数组时,我看不到内部数组中的数据.这是代码:
[runScoreDataArray addObject:[NSString stringWithString:currentUser]];
[runScoreDataArray addObject:[NSNumber numberWithDouble:mainScores.scoreTotal]];
NSLog(@"Array1 contains: %@", [runScoreDataArray description]);
// Now add the array runScoreDataArray to the Array highScoresArray
[highScoresArray addObject: runScoreDataArray];
// Empty the runScoresData Array after each run.
[runScoreDataArray removeAllObjects];
NSLog(@"Array2 contains: %@", [highScoresArray description]);
Run Code Online (Sandbox Code Playgroud)
runScoresDataArray的NSLog打印输出应该按原样读取:Array1包含:(USER1,34500)
NSLog for highScoresArray读取:ARRAY2包含:((),(),())
我希望在NSLog打印输出中看到每个数组元素的数据,而不是空括号.
问题:如何使用描述方法打印出数组数组的内容?
我有一个NSMutableArray.它的成员最终成为类中数组实例的成员.我想将NSMutable的实例化放入一个函数中并返回一个数组对象.如果我能做到这一点,我可以让我的一些代码更容易阅读.这可能吗?
这是我想弄清楚的.
//Definition:
function Objects (float a, float b) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithFloat:a]];
[array addObject:[NSNumber numberWithFloat:b]];
//[release array]; ????????
return array;
}
//Declaration:
Math *operator = [[Math alloc] init];
[operator findSum:Objects(20.0,30.0)];
Run Code Online (Sandbox Code Playgroud)
如果我在将消息发送到接收方之前实例化NSMutableArray,我的代码就会编译.我知道我可以有一个数组参数和方法.我遇到的问题是如何使用函数并用函数调用替换参数.任何帮助表示赞赏.我对这个概念感兴趣,而不是建议更换findSum方法.
场景 - 我有一个方法返回从NSMutableArray检索的对象,类似于下面的代码(已经简化了)(假设"myArray"和"currentIndex"是该方法所在的类的iVars):
- (MyObject *)getFromArray
{
return [myArray objectAtIndex:currentIndex];
}
Run Code Online (Sandbox Code Playgroud)
我应该这样打电话给自动释放吗?
- (MyObject *)getFromArray
{
return [[myArray objectAtIndex:currentIndex] autorelease];
}
Run Code Online (Sandbox Code Playgroud)
我知道数组在返回所述对象时会在对象上调用自动释放,但我的理解是自动释放的对象仅在调用者范围的持续时间内有效,在本例中为上述方法.因此,如果确定有问题的对象对我的方法的调用者有效,那么自动释放(如上面第二个代码片段所示)是否正确?只是试图确保我对自动释放如何工作的推理和理解是正确的.
谢谢!
我有一个情况.我有一个UITableviewController,在单元格中有文本字段.每个单元都是其中的文本字段.用户在文本字段中输入值,我将文本字段数据保存到NSMutableArray中.但是如果用户没有输入单个字段,那么我的数组会给出错误
"CoreAnimation:忽略异常:* - [NSMutableArray insertObject:atIndex:]:尝试将nil对象插入2".NSmutableArray不能具有空值.我将如何处理这种情况.任何帮助表示赞赏.
谢谢
您好我有这个NS对象:
@interface Checkin : NSObject {
NSString *name;
NSString *profID;
NSString *time;
NSString *lon;
NSString *lat;
NSString *country;
NSString *city;
NSString *place;
NSString *photoURL;
NSMutableArray *taggedID;
NSMutableArray *taggedName;
}
@property(nonatomic, retain)NSString *name;
@property(nonatomic, retain)NSString *profID;
@property(nonatomic, retain)NSString *time;
@property(nonatomic, retain)NSString *lon;
@property(nonatomic, retain)NSString *lat;
@property(nonatomic, retain)NSString *country;
@property(nonatomic, retain)NSString *city;
@property(nonatomic, retain)NSString *place;
@property(nonatomic, retain)NSString *photoURL;
@property(nonatomic, retain)NSMutableArray *taggedID;
@property(nonatomic, retain)NSMutableArray *taggedName;
@end
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个NSMutable Array并在其中添加几个"Checkins".
当我这样做时:
Checkin *checkinsA = [[NSObject alloc] init];
NSDictionary *decodedJson = result;
NSArray *users = [decodedJson …Run Code Online (Sandbox Code Playgroud) 我有这样NSMutableArray的UIImage命名camImages.然后我想得到scale一个图像的属性,但我似乎无法访问使用
[[camImages objectAtIndex:i] scale] //doesn't return the desired scale property
[camImages.objectAtIndex:i].scale //doesn't work (Error: Property 'scale' not found on object of type 'id')
Run Code Online (Sandbox Code Playgroud)
如果我有一个UIImage,那么可以获得该属性
UIImage *img;
img.scale //desired property
Run Code Online (Sandbox Code Playgroud)
我是iOS和Objective-C的新手,我怎样才能获得所需的属性?提前致谢!
编辑:
[[camImages objectAtIndex:i] scale]将返回
NSDecimalNumberBehaviors
规模
返回小数分隔符后允许的位数.(需要)
- (short)scale返回值小数分隔符后允许的位数.
而所需的规模是CGFloat类型:
的UIImage
规模
图像的比例因子.(只读)
@property(nonatomic,readonly)CGFloat scale讨论如果从名称中包含@ 2x修饰符的文件加载图像,则比例设置为2.0.您还可以在从Core Graphics图像初始化图像时指定显式比例因子.假设所有其他图像的比例因子为1.0.
如果将图像的逻辑大小(存储在size属性中)乘以此属性中的值,则可以获得图像的尺寸(以像素为单位).
我有一个包含数字的字符串数组.所以,如果有得到的方法NSNumber,从NSString它会很容易得到与数字数组:
[arrayOfStrings valueForKey:@"NSNumberValue"]
Run Code Online (Sandbox Code Playgroud)
但要将字符串转换为数字,您必须执行以下操作:
[NSNUmber numberWithInt:[string intValue]];
Run Code Online (Sandbox Code Playgroud)
我想知道如果没有手动迭代数组有没有办法做到这一点?
我想从NSUserDefaults更改对象get.
但是在执行以下代码时,应用程序崩溃并显示错误消息:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object’
Run Code Online (Sandbox Code Playgroud)
我有这个代码.
NSUserDefaults* pref = [NSUserDefaults standardUserDefaults];
NSMutableArray* data = [[NSMutableArray alloc]init];
data = [[pref objectForKey:@"Data"] mutableCopy];
for (NSArray* array in data) { //make object mutable
[array mutableCopy];
for (NSDictionary* dic in array) {
[dic mutableCopy];
}
}
[data[0][1] setObject:@"YES" forKey:@"isChecked"]; //for example
[pref setObject: data forKey: @"Data"];
[pref synchronize];
Run Code Online (Sandbox Code Playgroud)
数据结构:
NSMutableArray* arr1 = [@[[@{@"name":@"abc", @"isChecked":@"YES"} mutableCopy], [@{@"name":@"def", @"isChecked":@"NO"} mutableCopy]] mutableCopy];
NSMutableArray* …Run Code Online (Sandbox Code Playgroud) objective-c nsuserdefaults nsmutablearray nsmutabledictionary ios
nsmutablearray ×10
objective-c ×7
iphone ×4
cocoa-touch ×2
ios ×2
autorelease ×1
class ×1
function ×1
insert ×1
nsnumber ×1
nsobject ×1
nsstring ×1
oop ×1
properties ×1
uiimage ×1