循环遍历 NSArray 设置 objectAtIndex 值

发布于 2024-09-18 23:40:52 字数 1991 浏览 2 评论 0原文

我在 NSMutableArray 中有一堆 NSString。我已将它们添加到数组中,现在我想循环遍历数组,获取其中的每个对象(字符串)并将它们设置为公共值@“No Lessons”。

到目前为止,这就是我的循环方式:但是如何获取对象然后设置它呢?目前它相当简单,只是循环遍历数组,不改变任何东西。

NSInteger *count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA objectAtIndex:i];
    }

非常感谢任何帮助,谢谢。

编辑:

事实证明某处存在更大的问题。这是我正在使用的代码:

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);
    tueLessonArrayA = lessonArrayFuncTwo(tueP1A, tueP2A, tueP3A, tueP4A, tueP5A, tueP6A);
    tueLessonArrayB = lessonArrayFuncTwo(tueP1B, tueP2B, tueP3B, tueP4B, tueP5B, tueP6B);
    wedLessonArrayA = lessonArrayFuncTwo(wedP1A, wedP2A, wedP3A, wedP4A, wedP5A, wedP6A);
    wedLessonArrayB = lessonArrayFuncTwo(wedP1B, wedP2B, wedP3B, wedP4B, wedP5B, wedP6B);
    thuLessonArrayA = lessonArrayFuncTwo(thuP1A, thuP2A, thuP3A, monP4A, thuP5A, thuP6A);
    thuLessonArrayB = lessonArrayFuncTwo(thuP1B, thuP2B, thuP3B, thuP4B, thuP5B, thuP6B);
    friLessonArrayA = lessonArrayFuncTwo(friP1A, friP2A, friP3A, friP4A, friP5A, friP6A);
    friLessonArrayB = lessonArrayFuncTwo(friP1B, friP2B, friP3B, friP4B, friP5B, friP6B);

    NSInteger count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA replaceObjectAtIndex:i withObject:@"test"];
    }
}

所以现在我在这里使用一个函数来简单地将字符串放入几个数组中,然后返回循环,循环遍历数组并将文本放入对象中。你能看到任何问题吗?

加载应用程序时崩溃并给出 SIGBRT 错误。

I have a bunch of NSStrings in a NSMutableArray. I have added them to the array and now I want to loop through the array, getting each object in it (the strings) and set them to a common value @"No Lessons".

This is how I have the loop so far: But how can I get the object then set it? Currently its fairly simple, just getting looping through the array, not changing anything.

NSInteger *count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA objectAtIndex:i];
    }

Any help much appreciated, thanks.

EDIT:

Turns out there is a larger issue somewhere. This is the code I am using:

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);
    tueLessonArrayA = lessonArrayFuncTwo(tueP1A, tueP2A, tueP3A, tueP4A, tueP5A, tueP6A);
    tueLessonArrayB = lessonArrayFuncTwo(tueP1B, tueP2B, tueP3B, tueP4B, tueP5B, tueP6B);
    wedLessonArrayA = lessonArrayFuncTwo(wedP1A, wedP2A, wedP3A, wedP4A, wedP5A, wedP6A);
    wedLessonArrayB = lessonArrayFuncTwo(wedP1B, wedP2B, wedP3B, wedP4B, wedP5B, wedP6B);
    thuLessonArrayA = lessonArrayFuncTwo(thuP1A, thuP2A, thuP3A, monP4A, thuP5A, thuP6A);
    thuLessonArrayB = lessonArrayFuncTwo(thuP1B, thuP2B, thuP3B, thuP4B, thuP5B, thuP6B);
    friLessonArrayA = lessonArrayFuncTwo(friP1A, friP2A, friP3A, friP4A, friP5A, friP6A);
    friLessonArrayB = lessonArrayFuncTwo(friP1B, friP2B, friP3B, friP4B, friP5B, friP6B);

    NSInteger count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA replaceObjectAtIndex:i withObject:@"test"];
    }
}

So now here I am using a function to simply put the strings into several arrays, then it comes back tot he loop, where it loops through the arrays and puts the text into the objects. Can you see any issues?

Upon loading the app crashes giving a SIGBRT error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-09-25 23:40:52

我认为您应该退一步并重新考虑应用程序的设计,而不是弄清楚如何替换可变数组中特定索引处的对象。

看来您正在尝试设计一个管理一周“课程”的应用程序,并尝试通过暴力来实现:将本周的课程表示为字符串数组的集合。

您可能应该考虑可以使用哪些替代表示来简化应用程序,而不是这样做。例如,您不必将特定日期的课程表示为特定索引处的特定数组中的字符串,而是可以将计划的课程表示为课程类的实例,该课程不仅包含课程名称,还包含课程的日期以及是否是早上或下午课程:

typedef enum {
    LessonMonday=1,
    LessonTuesday=2,
    LessonWednesday=3,
    LessonThursday=4,
    LessonFriday=5,
} LessonDay;

typedef enum {
    LessonSessionA=1,
    LessonSessionB=2,
} LessonSession;

@interface Lesson : NSObject
@property (readwrite, copy) NSString *title;
@property (readwrite) LessonDay day;
@property (readwrite) LessonSesson session;

- (id)initWithTitle:(NSString *)title day:(LessonDay)day session:(LessonSession)session;
@end

现在您只需要表示实际安排的课程,例如在 scheduledLessons 集合中。您的用户界面可以告诉您何时安排课程并将该课程呈现为空课程,而无需您“填写”空课程;如果您想稍后为特定时段安排课程,只需创建一个新的 Lesson 实例来代表该时段中的课程并将其存储即可。这也使得更改现有课程的时段等操作变得更加容易,而无需到处乱扔数据:您可以只更改课程的标题、课程的日期或课程,而不会干扰其他课程。 (显然,在这样的事情中,您也想检查冲突,但同样,这很简单。)

在您了解了如何在代码中对此类事情进行建模的基础知识后,我强烈建议您看看使用 Core数据而不是手工完成。一旦您开始处理诸如持久性和搜索之类的事情,让核心数据实际为您实现大部分数据模型会比按照您在问题中显示的方式或我显示的方式要容易得多上面的你。例如,使用核心数据,我可能会将其建模为三个实体——课程、日和会话——其中一天与两个会话相关联,一个会话与一个课程相关联。

Rather than figure out how to replace objects at particular indexes in a mutable array, I think you should take a step back and reconsider the design of your application.

It appears that you're trying to design an application that manages "lessons" for a week, and are trying to do it by brute-force: Representing the week's lessons as a collection of arrays of strings.

Rather than doing it that way, you should probably consider what alternative representations you could use to simplify the application. For example, rather than represent a particular day's lessons as strings in particular arrays at particular indexes, you could instead represent scheduled lessons as instances of a Lesson class that not only has the name of the lesson, but also its day and whether it's a morning or afternoon lesson:

typedef enum {
    LessonMonday=1,
    LessonTuesday=2,
    LessonWednesday=3,
    LessonThursday=4,
    LessonFriday=5,
} LessonDay;

typedef enum {
    LessonSessionA=1,
    LessonSessionB=2,
} LessonSession;

@interface Lesson : NSObject
@property (readwrite, copy) NSString *title;
@property (readwrite) LessonDay day;
@property (readwrite) LessonSesson session;

- (id)initWithTitle:(NSString *)title day:(LessonDay)day session:(LessonSession)session;
@end

Now you only need to represent lessons that are actually scheduled, say in a scheduledLessons collection. Your user interface can tell when a lesson is scheduled and render that session as empty without you having to "fill in" an empty lesson; if you want to later schedule a lesson for a particular slot, just create a new instance of Lesson to represent the lesson in that slot and store it. This also makes it easier to do things like change an existing lesson's slot without shoving data all around: You can just change the title of a lesson, or the day of a lesson, or the session, without disturbing other lessons. (Obviously in something like this you would want to check for conflicts as well, but again, that's straightforward.)

After you understand the basics of how to model something like this in code I'd strongly urge you to take a look at using Core Data for it rather than doing it by hand. Once you start dealing with things like persistence and searching, it will just be much easier to have Core Data actually implement the bulk of your data model for you than either doing it the way you showed in your question, or the way I'm showing you above. For example, using Core Data I'd probably model this as three entities — Lesson, Day and Session — where a Day is associated with two Sessions and a Session is associated with a Lesson.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文