Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5748] Swift 4 issue with class_copyPropertyList ( describing the properties declared by the class) #48318

Closed
swift-ci opened this issue Aug 23, 2017 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself swift 4.0

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5748
Radar None
Original Reporter sandeep.sharma (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Swift4
Assignee @huonw
Priority Medium

md5: 6c7066a66ed3ffdcddaa192e8dc2def0

Issue Description:

In Swift 4 i am facing one compilation issue with below code .

var outCount:UInt32 = 0;
var rawProperties:UnsafeMutablePointer<objc_property_t?>! = class_copyPropertyList(clazz, &outCount)

Getting Compile time issue : Cannot convert value of type 'UnsafeMutablePointer<objc_property_t>?' (aka 'Optional<UnsafeMutablePointer<OpaquePointer>>') to specified type 'UnsafeMutablePointer<objc_property_t?>!' (aka 'ImplicitlyUnwrappedOptional<UnsafeMutablePointer<Optional<OpaquePointer>>>')

But when i am change it with ! to ? then able to compile but not getting class property count.

var rawProperties:UnsafeMutablePointer<objc_property_t?>? = class_copyPropertyList(clazz, &outCount)
Would any one can correct above code snippet?

Thanks

@huonw
Copy link
Mannequin

huonw mannequin commented Aug 23, 2017

The error message and the documentation https://developer.apple.com/documentation/objectivec/1418553-class_copypropertylist say that class_copyPropertyList returns a UnsafeMutablePointer<objc_property_t>?, but you have written UnsafeMutablePointer<objc_property_t?>?, with an extra ? inside the <...>. Could you say a bit more about what you mean by "not getting class property count"?

Also, attaching a complete xcode project or playground that demonstrates the full issue lets us help you better.

@swift-ci
Copy link
Collaborator Author

Comment by Sandeep (JIRA)

@huonw there is two things in attached screenshot if you seen.

  1. In screen shot line number 348, when i am using only UnsafeMutablePointer<objc_property_t>? & i am not getting the length of returned array in outCount(it's zero in all case) of passes class instance.

  2. In attached screenshot line number 349 is working with Swift 3 and i am getting length of array and it's working fine for me but after migrating whole project with Swift 4 & running it in Xcode9Beta6, i am getting compile time error mentioned in screenshot.

  3. Can we have a call at your time so that i can share my screen and show you.
    and for "not getting class property count" i mean length of array.

@swift-ci
Copy link
Collaborator Author

Comment by Sandeep (JIRA)

TestCopyProprty.zip
Please find this attached sample project with my problem.

@huonw
Copy link
Mannequin

huonw mannequin commented Aug 24, 2017

Thanks for uploading a project!

The problem is that the class_copyPropertyList is an Objective-C function, and so only understands Objective-C concepts. The properties of the MockupServer.Factory class are only Swift properties in your code, and need to be be marked with the @objc attribute for that function to know they exist. This can be done by placing @objc on each property (@objc var id: String = "") or by putting @objcMembers on the class itself (@objcMembers class Factory: NSObject) which is like marking everything in the class with @objc (properties and methods).

However, taking a step back, I'm not sure this is a good idea. This sort of reflection may be better handled in Swift with Mirrors, or some other approach that avoids being so dynamic.

(I'm closing as "invalid" because all the parts are behaving correctly here, but it is definitely confusing! It's one of the problems of having lots of Objective-C code around: some of it doesn't work as expected.)

@swift-ci
Copy link
Collaborator Author

Comment by Sandeep (JIRA)

@huonw Many Thansk Huon

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself swift 4.0
Projects
None yet
Development

No branches or pull requests

1 participant