Swift:XCode6 Beta 5在AppDelegate中给核心数据对象带来错误

时间:2021-03-20 20:52:49

I am developing an application in swift programming language. I was using the XCode6 Beta4 version and all the things were running smoothly and fine. I have updated the version to Beta5 today and I am getting the errors on core data objects which are:

我正在用swift编程语言开发一个应用程序。我正在使用XCode6 Beta4版本,所有的东西都运行得很顺利。我今天已将版本更新到Beta5,我在核心数据对象上得到的错误是:

  1. Type 'NSManagedObjectContext' does not conform to protocol 'BooleanType'.

    类型'NSManagedObjectContext'不符合协议'BooleanType'。

  2. Type 'NSManagedObjectModel' does not conform to protocol 'BooleanType'.

    类型'NSManagedObjectModel'不符合协议'BooleanType'。

  3. Type 'NSPersistentStoreCoordinator' does not conform to protocol 'BooleanType'.

    类型'NSPersistentStoreCoordinator'不符合协议'BooleanType'。

Screenshot of errors is also attached.

还附有错误的屏幕截图。

Swift:XCode6 Beta 5在AppDelegate中给核心数据对象带来错误

2 个解决方案

#1


14  

Actually you are getting the error that NSManagedObjectContext?, NSManagedObjectModel? and NSPersistentStoreCoordinator? do not confirm to BooleanType protocol. Notice ? question mark at the end of the type name.

实际上你得到的错误是NSManagedObjectContext?,NSManagedObjectModel?和NSPersistentStoreCoordinator?不确认为BooleanType协议。注意 ?类型名称末尾的问号。

So you are dealing with Optionals. Since Beta 5 Optionals does not conform to BooleanType protocol anymore.

所以你正在处理Optionals。由于Beta 5 Optionals不再符合BooleanType协议。

You need to check for nil explicitly, change:

您需要明确检查nil,更改:

if !_managedObjectContext {
    // ...
}

to:

至:

if _managedObjectContext == nil {
    // ...
}

And do the same for _managedObjectModel and _persistentStoreCoordinator.

并对_managedObjectModel和_persistentStoreCoordinator执行相同的操作。

From xCode 6 Beta 5 Release Notes:

来自xCode 6 Beta 5发行说明:

Optionals can now be compared to nil with == and !=, even if the underlying element is not Equatable.

现在可以使用==和!=将Optionals与nil进行比较,即使底层元素不是Equatable也是如此。

and

Optionals no longer conform to the BooleanType (formerly LogicValue) protocol, so they may no longer be used in place of boolean expressions (they must be explicitly compared with v != nil). This resolves confusion around Bool? and related types, makes code more explicit about what test is expected, and is more consistent with the rest of the language. Note that ImplicitlyUnwrappedOptional still includes some BooleanType functionality. This issue will be resolved in a future beta.

Optionals不再符合BooleanType(以前的LogicValue)协议,因此它们可能不再用于代替布尔表达式(它们必须与v!= nil明确比较)。这解决了Bool的混乱局面?和相关类型,使代码更清楚地表明预期的测试,并且与语言的其余部分更加一致。请注意,ImplicitlyUnwrappedOptional仍包含一些BooleanType功能。此问题将在未来的测试版中得到解决。

#2


1  

Try if _managedObjectContext == nil instead of !if _managedObjectContext and do the same to persistentStoreCoordinator it's because apple changed something with BooleanType (and not just with if) with xCode beta 5 update.

尝试使用_managedObjectContext == nil而不是!if _managedObjectContext并对persistentStoreCoordinator执行相同的操作,因为apple通过xCode beta 5 update更改了BooleanType(而不仅仅是if)。

#1


14  

Actually you are getting the error that NSManagedObjectContext?, NSManagedObjectModel? and NSPersistentStoreCoordinator? do not confirm to BooleanType protocol. Notice ? question mark at the end of the type name.

实际上你得到的错误是NSManagedObjectContext?,NSManagedObjectModel?和NSPersistentStoreCoordinator?不确认为BooleanType协议。注意 ?类型名称末尾的问号。

So you are dealing with Optionals. Since Beta 5 Optionals does not conform to BooleanType protocol anymore.

所以你正在处理Optionals。由于Beta 5 Optionals不再符合BooleanType协议。

You need to check for nil explicitly, change:

您需要明确检查nil,更改:

if !_managedObjectContext {
    // ...
}

to:

至:

if _managedObjectContext == nil {
    // ...
}

And do the same for _managedObjectModel and _persistentStoreCoordinator.

并对_managedObjectModel和_persistentStoreCoordinator执行相同的操作。

From xCode 6 Beta 5 Release Notes:

来自xCode 6 Beta 5发行说明:

Optionals can now be compared to nil with == and !=, even if the underlying element is not Equatable.

现在可以使用==和!=将Optionals与nil进行比较,即使底层元素不是Equatable也是如此。

and

Optionals no longer conform to the BooleanType (formerly LogicValue) protocol, so they may no longer be used in place of boolean expressions (they must be explicitly compared with v != nil). This resolves confusion around Bool? and related types, makes code more explicit about what test is expected, and is more consistent with the rest of the language. Note that ImplicitlyUnwrappedOptional still includes some BooleanType functionality. This issue will be resolved in a future beta.

Optionals不再符合BooleanType(以前的LogicValue)协议,因此它们可能不再用于代替布尔表达式(它们必须与v!= nil明确比较)。这解决了Bool的混乱局面?和相关类型,使代码更清楚地表明预期的测试,并且与语言的其余部分更加一致。请注意,ImplicitlyUnwrappedOptional仍包含一些BooleanType功能。此问题将在未来的测试版中得到解决。

#2


1  

Try if _managedObjectContext == nil instead of !if _managedObjectContext and do the same to persistentStoreCoordinator it's because apple changed something with BooleanType (and not just with if) with xCode beta 5 update.

尝试使用_managedObjectContext == nil而不是!if _managedObjectContext并对persistentStoreCoordinator执行相同的操作,因为apple通过xCode beta 5 update更改了BooleanType(而不仅仅是if)。