我在NSMutableArray中有一堆NSStrings.我已将它们添加到数组中,现在我想循环遍历数组,获取其中的每个对象(字符串)并将它们设置为公共值@"No Lessons".
这就是我到目前为止的循环方式:但是如何获得对象然后设置呢?目前它相当简单,只是循环遍历数组,而不是改变任何东西.
NSInteger *count = [monLessonArrayA count];
for (int i = 0; i < count; i++) {
[monLessonArrayA objectAtIndex:i];
}
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢,谢谢.
编辑:
事实证明,某处有一个更大的问题.这是我正在使用的代码:
NSMutableArray* lessonArrayFuncTwo(id a, id b, id c, id d, id e, id f) {
NSMutableArray* lessonsArray = [[NSMutableArray alloc] init];
[lessonsArray addObject:a];
[lessonsArray addObject:b];
[lessonsArray addObject:c];
[lessonsArray addObject:d];
[lessonsArray addObject:e];
[lessonsArray addObject:f];
return lessonsArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
monLessonArrayA = lessonArrayFuncTwo(monP1A, monP2A, monP3A, monP4A, monP5A, monP6A);
monLessonArrayB = lessonArrayFuncTwo(monP1B, monP2B, monP3B, monP4B, monP5B, monP6B); …Run Code Online (Sandbox Code Playgroud) 如果我有一个带有几个字典对象的NSArray(或可变数组)(每个字典都是Person),就像这样:
[
{ forename: chris, surname: smith, age: 22 }
{ forename: paul, surname: smith, age: 24 }
{ forename: dave, surname: jones, age: 21 }
{ forename: alan, surname: jones, age: 26 }
{ forename: cecil, surname: reeves, age: 32 }
{ forename: pablo, surname: sanchez, age: 42 }
]
Run Code Online (Sandbox Code Playgroud)
我如何将它分成四个数组的数组,按姓氏(并在该名称中)排序,如下所示:
[
[
{ forename: alan, surname: jones, age: 26 }
{ forename: dave, surname: jones, age: 21 }
]
[
{ forename: cecil, surname: reeves, age: 32 } …Run Code Online (Sandbox Code Playgroud) 我真的需要一些帮助!
我试图将数组从一个视图控制器传递到另一个视图控制器.我认为后者是一个"孩子"视图控制器?
我的代码如下:
MainViewController.h:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface HelloWorldIOS4ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, AVAudioPlayerDelegate> {
NSMutableArray *countProductCode;
UIPopoverController *detailViewPopover;
}
@property (nonatomic, retain) NSMutableArray *countProductCode;
@property (nonatomic, retain) UIPopoverController *detailViewPopover;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
...
@end
Run Code Online (Sandbox Code Playgroud)
MainViewController.m
#import "HelloWorldIOS4ViewController.h"
#import "JSON.h"
#import "PopoverContentViewController.h"
@implementation HelloWorldIOS4ViewController
@synthesize detailViewPopover;
@synthesize countProductCode;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSLog(@"RETURN: %@", results);
[countProductCode removeAllObjects];
NSArray *products = …Run Code Online (Sandbox Code Playgroud) NSArray如果删除第一个元素(类似于ArrayList在Java中的工作方式),是否会将其元素向下移动(类似于java中的数组)?
目标:使用优雅代码获取包含给定NSDictionary的唯一键的NSArray
带当前工作解决方案的示例代码
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"a", [NSNumber numberWithInt:2], @"b", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"b", [NSNumber numberWithInt:4], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:6], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:7], @"b", [NSNumber numberWithInt:8], @"a", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:8], @"c", [NSNumber numberWithInt:9], @"b", nil],
nil];
// create an NSArray of all the dictionary keys within the NSArray *data
NSMutableSet *setKeys = [[NSMutableSet alloc] init];
for (int i=0; i<[data count]; i++) {
[setKeys addObjectsFromArray:[[data objectAtIndex:i] allKeys]]; …Run Code Online (Sandbox Code Playgroud) 我添加对象后,NSMutableArray它变成了nil(我检查了NSLog)
码
guests = [[NSMutableArray alloc]init];
guests = [localDictionary objectForKey:@"guestsArray"];
[guests addObject:@"1"];
[guests addObject:@"12"];
[guests addObject:@"31"];
if(guests == nil)
{
NSLog(@"WHY?");
}
Run Code Online (Sandbox Code Playgroud)
你知道为什么会这样吗?我该怎么检查?
我试图用整数填充我的数组.但是,对我来说有些奇怪的事情并不像我想的那么容易.以下是对所发生情况的描述.
代码的最后一行给了我错误."不允许将int隐式转换为NSArray"
//.h file
{
NSArray storeDisk[15];
}
property int virdCount;
property (nonatomic, strong)NSArray *storeDisk;
//.m file
virdCount+=virdCount;
storeDisk[0]=virdCount;
Run Code Online (Sandbox Code Playgroud) 如果我想创建一个2x2数组,我会这样做:
NSArray *theArray = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"String1",@"String2",nil],
[NSArray arrayWithObjects:@"String3",@"String4",nil],
nil];
Run Code Online (Sandbox Code Playgroud)
但是,如果我想以另一种方式创建它,我会收到错误"表达式不可分配"
[self theArray] = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"String1",@"String2",nil],
[NSArray arrayWithObjects:@"String3",@"String4",nil],
nil];
Run Code Online (Sandbox Code Playgroud)
在上面的例子中使用我的[self theArray]的正确方法是什么?
我以前从未见过这种情况.我有一个NSArray填充了6个对象.我只使用一个平均的简单for循环遍历数组抓取值:
for (int i = 0; i <= self.myArray.count; i++) {
CustomClass *stopTimes = [self.myArray objectAtIndex:i];
NSLog(@"Hey this number %d: at this time %lld", i, stopTimes.theTimeRange.start.value);
}
Run Code Online (Sandbox Code Playgroud)
当这个循环运行时,每次都会因为只访问数组之外的1个索引而崩溃.在这种情况下有6个项目,它崩溃试图访问第六个.
所以,我去了LLDB并确认数组中确实有6个对象:
(lldb) po self.myArray
<__NSArrayM 0x16bf8620>(
<NeededObject: 0x16bf9f40>,
<NeededObject: 0x16ceO1c0>,
<NeededObject: 0x16ce4268>,
<NeededObject: 0x16cf0b75>,
<NeededObject: 0x16b06d22>,
<NeededObject: 0x16b02240>
)
Run Code Online (Sandbox Code Playgroud)
但是......我在循环中的NSLog只打印出5个对象.我不知道为什么循环试图访问它的界限之外.有一些黑客可以解决这个问题(例如设置i = 1等).
我是快速编程的新手.我正在尝试学习plist文件.我有一个plist文件,其中包含国家,州和城市的数据.我想迭代plist文件的数据.但我无法理解如何创建数组或字典来存储国家,州和城市的数据.你能帮我讲一下如何操作plist文件的以下数据吗?
{
Country = (
{
CountryName = India;
State = (
{
City = (
Ahmedabad,
Vadodara,
Surat,
Aanand,
Bharuch
);
StateName = Gujrat;
},
{
City = (
Mumbai,
Pune,
Nagpur,
Nasik,
Thane
);
StateName = Maharastra;
},
{
City = (
Kochi,
Kanpur,
Alleppey,
Thrissur,
Thiruvananthapuram
);
StateName = Kerala;
}
);
},
Run Code Online (Sandbox Code Playgroud)