Ios在Swift 2.2中为NSManagedObject子类创建自定义init

时间:2016-06-28 21:17:37

标签: ios swift2 subclass nsmanagedobject init

使用IOS9和Swift2.2

我想创建一个空的init()作为创建NSManagedObject变量,如

let NamTblVar = NamTblCls()
NamTblVar.Nam1Var = "TrlTxt1"
NamTblVar.Nam2Var = "TrlTxt2"
NamTblVar.Nam3Var = "TrlTxt2"
NamTblVar.SevDataFnc(NamTblVar)

并在自定义NSManagedObject类

中编写所有保存和检索函数

我正在尝试创建自定义类但不能初始化()
获取错误:必须调用超类“NSManagedObject”的指定初始化程序

我试过

class NamTblCls: NSManagedObject
{
    let ContxtVar = (UIApplication.sharedApplication().delegate as! AppDgtCls).managedObjectContext

    init()
    {
        super.init()

        super.init(entity: NSEntityDescription.entityForName("NamTbl", inManagedObjectContext: ContxtVar)!, insertIntoManagedObjectContext: ContxtVar)
    }

}

1 个答案:

答案 0 :(得分:1)

从技术上讲,这是你可以做到的一种方式:

    using (var connection = factory.CreateConnection())
    {
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            channel.BasicConsume(queue: "hello",
                                 noAck: false,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

let title = NSManagedObjectContextConcurrencyType(rawValue: 0) let test: NSManagedObjectContext = NSManagedObjectContext(concurrencyType: title!) class NamTblCls: NSManagedObject { //let ContxtVar = (UIApplication.sharedApplication().delegate).managedObjectContext init(){ super.init(entity: NSEntityDescription.entityForName("String", inManagedObjectContext: test)!, insertIntoManagedObjectContext: test) } } title用于在操场上进行测试)

但实际上,您应该像这样设置test

class

初始化课程时创建class NamTblCls: NSManagedObject { //let ContxtVar = (UIApplication.sharedApplication().delegate).managedObjectContext init(entityName: String, someName: NSManagedObjectContextConcurrencyType){ let test: NSManagedObjectContext = NSManagedObjectContext(concurrencyType: someName) super.init(entity: NSEntityDescription.entityForName(entityName, inManagedObjectContext: test)!, insertIntoManagedObjectContext: test) } } entityName