Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Xcode如何使用包含自定义对象的自定义对象持久化数据?_Ios_Swift_Core Data_Nsuserdefaults_Nscoding - Fatal编程技术网

iOS Xcode如何使用包含自定义对象的自定义对象持久化数据?

iOS Xcode如何使用包含自定义对象的自定义对象持久化数据?,ios,swift,core-data,nsuserdefaults,nscoding,Ios,Swift,Core Data,Nsuserdefaults,Nscoding,这里是Android开发者和iOS新手。我有一个应用程序正在迁移到iOS。我正在同时学习Xcode和Swift,所以请温柔一点!:) 我的问题是,我只想保存/检索一个类,如下所示(在Java中): 公共类CardDeck实现可包裹{ 私人ArrayList卡; 私密地点; 公共卡片组(){ this.cards=new ArrayList(); 这个位置=0; //this.numCards=0; } 公共卡片组(ArrayList卡片){ 这个。卡片=卡片; 这个位置=0; //this.nu

这里是Android开发者和iOS新手。我有一个应用程序正在迁移到iOS。我正在同时学习Xcode和Swift,所以请温柔一点!:)

我的问题是,我只想保存/检索一个类,如下所示(在Java中):

公共类CardDeck实现可包裹{
私人ArrayList卡;
私密地点;
公共卡片组(){
this.cards=new ArrayList();
这个位置=0;
//this.numCards=0;
}
公共卡片组(ArrayList卡片){
这个。卡片=卡片;
这个位置=0;
//this.numCards=cards.size();
}
受保护的CardDeck(包裹中){
卡片=in.createTypedArrayList(Profile.CREATOR);
//numCards=in.readInt();
location=in.readInt();
}
}
我想保存/检索包含配置文件数组的CardDeck对象。我最初的阅读让我觉得,无论是使用UserDefaults还是NSCoding都无法做到这一点,因为plist文件不能包含像Profile这样的自定义数据类型。我说对了吗?UserDefaults用于标准数据类型(bool、int、String等),NSCoding可以处理自定义对象,如CardDeck,但CardDeck只能包含标准数据类型的成员

你会如何处理我的问题?核心数据

我在Android中通过使用
SharedReferences
Gson.toJson(cardDeck)
处理了这个问题


谢谢你的阅读

CoreData是在SQLLite之上构建的ORM。听起来你想要的可能太过分了

您可以声明您的类是可编码的:

类别卡片组:可编码 { }

然后只需使用JSONECODER、JSONDecoder将它们编码和解码为数据对象,这些数据对象可以转换为字符串,然后存储在文件或用户首选项中。CardDeck中的任何自定义类型也需要是JSONCoadeble


JSONCoadeble是一种协议(即Java术语中的接口),可以作为现有类的扩展来实现(在Java中不能这样做,在C#中有点像can)。

谢谢Andrew!我会调查JSONCABLED。听起来正是我需要的。非常感激。再次感谢。在椰子荚上找到的。哦!我现在明白了,你不必使用吊舱。您可以使用JSONeCoder/decoder,它是Swift 4中可编码接口的一部分。是的,这是正确的。JSONCABLE是Swift 4的一项功能。
public class CardDeck implements Parcelable{

  private ArrayList<Profile> cards;

  private int location;

  public CardDeck(){
    this.cards = new ArrayList<>();
    this.location = 0;
    //this.numCards = 0;
  }

  public CardDeck(ArrayList<Profile> cards) {
    this.cards = cards;
    this.location = 0;
    //this.numCards = cards.size();
  }

  protected CardDeck(Parcel in) {
    cards = in.createTypedArrayList(Profile.CREATOR);
    //numCards = in.readInt();
    location = in.readInt();
  }
}