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-2801] Need a way to implement Sequence from Obj-C code. #45405

Open
swift-ci opened this issue Sep 29, 2016 · 3 comments
Open

[SR-2801] Need a way to implement Sequence from Obj-C code. #45405

swift-ci opened this issue Sep 29, 2016 · 3 comments
Labels
improvement standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2801
Radar None
Original Reporter mikelehen (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: 9396e06997f092bc2380dacf6f4911a9

Issue Description:

See http://stackoverflow.com/questions/39757563/how-do-i-implement-sequence-to-allow-swifts-for-in-syntax-from-objective-c for details.

But basically, it doesn't seem as there's any way to write a StringList class in Obj-C that can be iterated in swift with for-in syntax:

let list = StringList()
for y: String in list {
  ...
}

It seems if I derive from NSEnumerator<NSString *>, then for-in works except the generic type is lost, and so declaring y as String gives a "'NSFastEnumerationIterator.Element' (aka 'Any') is not convertible to 'String'" error.

@belkadan
Copy link
Contributor

@dabrahams, did we ever figure out if it was a good idea to use NSFastEnumeration to implement Sequence?

@belkadan
Copy link
Contributor

I guess the importer can't do it automatically, but we could derive Sequence from NSFastEnumeration instead of making people write the makeIterator call with NSFastEnumerationIterator explicitly.

@swift-ci
Copy link
Collaborator Author

Comment by Michael Lehenbauer (JIRA)

Any chance the generic type could also be preserved somehow? I'd like my for-in to work without having to as! cast from Any to String...

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants