Swift对象必须是object类型

时间:2015-03-19 15:01:56

标签: ios swift ios8 realm

在构建我的领域模型之初,我的League类中只有一个整数引用了它的运动id。然后,为了获得给定运动的所有相关联盟,我在其Realm模型中定义了一个反比关系。

这导致出现错误消息,指出dynamic var sport必须是Sport类型,而不是Int。所以我再次改变它,现在我试图通过传递一个Sport对象来初始化一个联盟,它可以用来建立联盟。

现在我又收到了另一个错误,我根本找不到任何错误。我不太了解错误信息,堆栈跟踪也不能帮助我找到更多信息。

class Sport: RLMObject {
  dynamic var id: Int        = 0
  dynamic var sortOrder: Int = 0
  dynamic var name: String   = ""
  dynamic var icon: String   = ""
  var leagues: [League] {
    return linkingObjectsOfClass("League", forProperty: "sport") as! [League]
  }

  override class func primaryKey() -> String {
    return "id"
  }

  init(json: JSON) {
    super.init()
    id        = json["id"].intValue
    name      = json["name"].stringValue
    icon      = json["pictogram"].stringValue
    sortOrder = json["sortOrder"].intValue
  }
}

我的班级联赛:

class League: RLMObject {
  dynamic var id: Int               = 0
  dynamic var sport: Sport?         = nil
  // Other properties

  override class func primaryKey() -> String {
    return "id"
  }

  init(json: JSON, sport: Sport) {
    super.init()
    id          = json["id"].intValue
    self.sport  = sport
    // Initializing other properties
  }
}

错误讯息:

2015-03-19 16:01:41.393 MSUapp[7012:142456]  Terminating app due to uncaught exception 'Invalid value', reason: 'object must be of type object'
 First throw call stack:
(
    0   CoreFoundation                      0x0075d686 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x0238da97 objc_exception_throw + 44
    2   MSUapp                              0x000e41e0 _ZL15RLMPreconditionbP8NSStringS0_z + 211
    3   MSUapp                              0x000e9cb1 _ZN12_GLOBAL__N_123validate_property_valueEP11RLMPropertyP11objc_objectP8NSString + 485
    4   MSUapp                              0x000e9077 _ZN12_GLOBAL__N_134update_query_with_value_expressionEP9RLMSchemaP15RLMObjectSchemaRN7tightdb5QueryEP8NSStringP11objc_objectP21NSComparisonPredicate + 2691
    5   MSUapp                              0x000e4a18 _ZN12_GLOBAL__N_127update_query_with_predicateEP11NSPredicateP9RLMSchemaP15RLMObjectSchemaRN7tightdb5QueryE + 1713
    6   MSUapp                              0x000e42b2 _Z27RLMUpdateQueryWithPredicatePN7tightdb5QueryEP11NSPredicateP9RLMSchemaP15RLMObjectSchema + 169
    7   MSUapp                              0x000e1e2b RLMGetObjects + 329
    8   MSUapp                              0x000d7d62 +[RLMObject objectsWithPredicate:] + 117
    9   MSUapp                              0x000be58e _TFC6MSUapp24AllLeaguesViewController9tableViewfS0_FTCSo11UITableView21numberOfRowsInSectionSi_Si + 430
    10  MSUapp                              0x000be6fb _TToFC6MSUapp24AllLeaguesViewController9tableViewfS0_FTCSo11UITableView21numberOfRowsInSectionSi_Si + 75
    11  UIKit                               0x0147f21f -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2803
    12  UIKit                               0x014835da -[UITableViewRowData numberOfRows] + 98
    13  UIKit                               0x012a753c -[UITableView noteNumberOfRowsChanged] + 176
    14  UIKit                               0x012a6a41 -[UITableView reloadData] + 1837
    15  MSUapp                              0x000bdb9e _TFC6MSUapp24AllLeaguesViewController24didFinishLoadingJSONDatafS0_FT_T_ + 238
    16  MSUapp                              0x000bf664 _TTWC6MSUapp24AllLeaguesViewControllerS_16JSONDataReceiverS_FS1_24didFinishLoadingJSONDataUS1___fQPS1_FT_T_ + 36
    17  MSUapp                              0x0009490d _TFFC6MSUapp14JSONDataSource18loadRemoteJsonDataFS0_FTSSCS_19GETParameterBuilderFCSo6NSDataT__T_U_FTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T_ + 2461
    18  MSUapp                              0x00092d32 _TPA__TFFC6MSUapp14JSONDataSource18loadRemoteJsonDataFS0_FTSSCS_19GETParameterBuilderFCSo6NSDataT__T_U_FTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T_ + 162
    19  Alamofire                           0x004a697a _TFFFC9Alamofire7Request8responseFDS0_FT5queueGSqCSo8NSObject_10serializerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqCSo6NSData__TGSqPSs9AnyObject__GSqCSo7NSError__17completionHandlerFTS2_GSqS3__GSqPS5___GSqS6___T__DS0_U_FT_T_U_FT_T_ + 394
    20  Alamofire                           0x004a0d17 _TPA__TFFFC9Alamofire7Request8responseFDS0_FT5queueGSqCSo8NSObject_10serializerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqCSo6NSData__TGSqPSs9AnyObject__GSqCSo7NSError__17completionHandlerFTS2_GSqS3__GSqPS5___GSqS6___T__DS0_U_FT_T_U_FT_T_ + 135
    21  Alamofire                           0x004a4298 _TTRXFo__dT__XFdCb__dT__ + 40
    22  libdispatch.dylib                   0x02a8b5ea _dispatch_call_block_and_release + 15
    23  libdispatch.dylib                   0x02aadbef _dispatch_client_callout + 14
    24  libdispatch.dylib                   0x02a936bb _dispatch_main_queue_callback_4CF + 993
    25  CoreFoundation                      0x006b685e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    26  CoreFoundation                      0x00674680 __CFRunLoopRun + 2256
    27  CoreFoundation                      0x00673aeb CFRunLoopRunSpecific + 443
    28  CoreFoundation                      0x0067391b CFRunLoopRunInMode + 123
    29  GraphicsServices                    0x0526f2c9 GSEventRunModal + 192
    30  GraphicsServices                    0x0526f106 GSEventRun + 104
    31  UIKit                               0x01192366 UIApplicationMain + 1526
    32  MSUapp                              0x000aba94 main + 180
    33  libdyld.dylib                       0x02ad8ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我错过了什么?我没有将体育作为对象传递,而是尝试做以下事情来设置这项运动:

   sport = Sport.objectsWhere("id = %i", json["sport"].intValue).firstObject() as Sport

但是那产生了完全相同的例外。

1 个答案:

答案 0 :(得分:0)

该解决方案与this line完全无关。

TableViewController内,其中Sport是该部分,League是其单元格,我根据一项运动获得了以下代码来获得联赛:

var league = League.objectsWhere("sport = %i", sportsList[indexPath.section].id) as League
// sportsList is of type [Sport]

我没有考虑过必须改变这条线,但是一旦它成为一种关系,我就不得不将其更改为以下内容:

var league = League.objectsWhere("sport = %@", sportsList[indexPath.section]) as League

这会插入整个对象,而不仅仅是id。我想我已经习惯在网络开发中使用MySQL了。