NSOrderedSet不会构建在由XCode生成的iPhone类 [英] NSOrderedSet wont build in iPhone class generated by XCode

查看:134
本文介绍了NSOrderedSet不会构建在由XCode生成的iPhone类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lion上的XCode 4.1中创建了一个具有一对多关系的简单数据模型。



为此关系生成的属性类型是NSOrderedSet,



我搜索文档以了解这是一种为Mac OS 10.7(Lion)添加的新类型的集合类,特别适用于被管理的对象(我只是习惯了苹果的工作方式:基本的东西作为一种集合类型的语言绑定到操作系统版本...有趣)



这很有意义,NdOrderedSet显然是NSSet和NSArray的功能的组合 - 也就是说,它有一个像数组一样的索引顺序,每个元素都像一个集合。尼斯。但是,我的问题是:


  1. 为什么不编译?

  2. 是XCode生成的东西,不会编译?



    我要自己在#1自己赃物:我的SDK是iOS 4.3,早于狮子不是Mac OS,而是iOS),并且根本没有这个新的集合类。
    好​​吧,公平了,但#2代表:为什么XCode为我生成它?为什么不检测我的SDK和生成的东西工作?为什么不给我一个选择?



    这带来了#3和#4


  3. 解决方法?我应该将其更改为NSSet或NSArray? XCode如何在4.0之前生成这些东西?


  4. 如果我使用NSOrderedSet(即让它工作在某种程度上)将向后兼容以前的iOS版本?



解决方案

Core Data有序关系仅适用于iOS 5,而不适用于iOS 4.3。
您需要iOS 5 SDK,它仅在Xcode 4.2上可用。从Xcode 4.1切换到Xcode 4.2,你应该很好。



3:解决方法是使用Xcode 4.2和目标iOS 5.如果你仍然想要目标iOS 4.x并有顺序关系,继续阅读。



4:你不太可能能够得到NSOrderedSet在iOS 4.3中工作。 Cocoa Touch中缺少类。所以你需要从头重新实现,这是绝对可行的。什么是不是 doable,是要获得核心数据使用它。所以忘记它。



现在,值得一提的是,它可以定义与旧核心数据的有序关系。它需要更多的工作,但它绝对是可行的。下面是一个例子:



想象一个 Meal 和一个课程实体。膳食与课程有有序的关系,因为在膳食期间的课程以特定的顺序出现。你不想在主课程之前吃甜点!



这个想法是定义一个第三个助手实体,代表课程的会员资格。让我们调用该实体 CourseInMeal 。现在设置以下关系:

 课程< ---------> CourseInMeal<< ------------------ Meal 

其中双箭头代表关系的to-many

这些关系基本上说,一门课程可以在几餐中出现,而一餐可以有几门课程。



只剩下这是代表有序关系。为此,只需向 CourseInMeal 实体添加一个属性,也许称为 position



现在,当你想定义一个新的餐,你为每个课程创建一个 CourseInMeal 的新实例,如: p>

  CourseInMeal * newMealMembership = [CourseInMeal insertInManagedObjectContext:self.moc]; 
newMealMembership.course = myDessert;
newMealMembership.meal = myBirthdayMeal;
newMealMembership.position = 3; // or whatever
[self.moc save:NULL];

Voilà,你在普通的核心数据中有一个有序关系。


I created a simple data model with a 1-many relationship in XCode 4.1 on Lion.

The type of the property generated for this relationship is NSOrderedSet, which fails to compile because the type is unknown.

I searched the documentation to learn that this is a new kind of collection classes added for Mac OS 10.7 (Lion), especially useful in managed objects (I'm just getting used to the way Apple works: something as basic as a collection type in the language is bound to an OS release... interesting)

This makes sense, NdOrderedSet is apparently a combination of the features of NSSet and NSArray - that is, it has an indexed order like an array, with each element unique like a set. Nice. But, my questions are:

  1. Why won't it compile?
  2. Why is XCode generating something that won't compile?

    I'm going to take a swag at #1 myself: My SDK is iOS 4.3, which predates Lion (and is not Mac OS but iOS) and simply does not have this new collection class. okay, fair enough, but #2 stands: why did XCode generate it for me? Why not detect my SDK and generate something that works? Why not give me a choice?

    This brings me to #3 and #4

  3. What's the workaround? Should I change it to NSSet or NSArray? How did XCode generate these things before 4.0?

  4. If I were to use NSOrderedSet (ie get it to work somehow) would it be backward compatible with prior iOS versions?

解决方案

Core Data ordered relationships are only available on iOS 5, not iOS 4.3. You need the iOS 5 SDK which is only available on Xcode 4.2. Switch from Xcode 4.1 to Xcode 4.2 and you should be fine.

3: the workaround is to use Xcode 4.2 and to target iOS 5. If you still want to target iOS 4.x and have ordered relationship, read on.

4: you are unlikely to be able to get NSOrderedSet to work in iOS 4.3. The class is simply missing from Cocoa Touch. So you would need to reeimplement it from scratch, which is definitely doable. What is not doable, is to then get Core Data to use it. So forget it.

Now, it may be worth mentioning that it is possible to define ordered relationships with the "old" Core Data. It requires a bit more work, but it's definitely doable. Here is an example:

Imagine a Meal and a Course entities. A meal has an ordered relationship to courses, as courses during a meal come up in a specific order. You don't want to eat your dessert before the main course!

The idea is to define a third helper entity that represents the course membership in the meal. Let's call that entity CourseInMeal. Now set up the following relationships:

Course <--------->> CourseInMeal <<----------> Meal

Where the double arrow represents the "to-many" end of the relationships.

These relationships basically says that a Course can be present in several meals, and that a Meal can have several courses.

The only remaining this is to represent the ordered relationship. To do so, simply add an attribute to the CourseInMeal entity, perhaps called position.

Now when you want to define a new meal you create a new instance of CourseInMeal for each course in the meal, with something like:

CourseInMeal *newMealMembership = [CourseInMeal insertInManagedObjectContext:self.moc];
newMealMembership.course = myDessert;
newMealMembership.meal = myBirthdayMeal;
newMealMembership.position = 3; // or whatever
[self.moc save:NULL];

Voilà, you have an ordered relationship in plain old Core Data.

这篇关于NSOrderedSet不会构建在由XCode生成的iPhone类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆