Skip to content

Commit

Permalink
Fix build issues
Browse files Browse the repository at this point in the history
1. When running `make build` the compiler complains that it “can’t type check this expression in reasonable time” in `ObjectiveCInitExtension.swift`. I put the expression in an explicitly declared variable to help out the compiler’s tiny brain.
2. The test `testPolymorphicPropWithBool` is failing. Looking at the code, we test that an `NSNumber` is a bool via:
```
if ([value isKindOfClass:[NSNumber class]] && strcmp([value objCType], @encode(BOOL)) == 0) {
                    self->_polymorphicProp = [EverythingPolymorphicProp  objectWithBoolean:[value boolValue] & 0x1];
                }
```
However, I’m seeing weird behavior where `@encode(BOOL)` is not returning `c` as [Apple documentation](https://developer.apple.com/documentation/objectivec/bool) says it should: ```
BOOL is explicitly signed so @encode(BOOL) is c rather than C even if -funsigned-char is used.
```

Here is what I was seeing in the debugger:
```
(lldb) po @encode(BOOL)
"B"
(lldb) po [@yES objCType]
"c"
(lldb) po [@no objCType]
"c"
```

I am not sure why this is retuning `B`, but it seems a safer check would be:
```
if ([value isKindOfClass:[NSNumber class]] && strcmp([value objCType], [[NSNumber numberWithBool:0] objCType]) == 0) {
    self->_polymorphicProp = [EverythingPolymorphicProp  objectWithBoolean:[value boolValue] & 0x1];
}
```
I added this change to fix the tests.
  • Loading branch information
rcancro committed Sep 9, 2024
1 parent a2fe6e1 commit 067fb8b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Core/ObjectiveCInitExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ extension ObjCModelRenderer {
}

case .boolean:
return ObjCIR.ifStmt("[\(rawObjectName) isKindOfClass:[NSNumber class]] && strcmp([\(rawObjectName) objCType], @encode(BOOL)) == 0") {
return ObjCIR.ifStmt("[\(rawObjectName) isKindOfClass:[NSNumber class]] && strcmp([\(rawObjectName) objCType], [[NSNumber numberWithBool:0] objCType]) == 0") {
transformToADTInit(renderPropertyInit(propertyToAssign, rawObjectName, keyPath: keyPath, schema: schema, firstName: firstName, counter: counter, dirtyPropertyName: dirtyPropertyName, needsTypeCheck: false))
}
case .array(itemType: _):
Expand Down
3 changes: 2 additions & 1 deletion Sources/Core/ObjectiveCNSCodingExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
extension ObjCModelRenderer {
func renderInitWithCoder() -> ObjCIR.Method {
return ObjCIR.method("- (instancetype)initWithCoder:(NSCoder *)aDecoder") {
[
let body: [String] = [
self.isBaseClass ? ObjCIR.ifStmt("!(self = [super init])") { ["return self;"] } :
"if (!(self = [super initWithCoder:aDecoder])) { return self; }",
self.properties.filter { (_, schema) -> Bool in
Expand All @@ -35,6 +35,7 @@ extension ObjCModelRenderer {
},
"return self;",
]
return body
}
}

Expand Down

0 comments on commit 067fb8b

Please sign in to comment.