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-8408] String extension requires self to access method of in built NSString extension #3659

Closed
swift-ci opened this issue Jul 30, 2018 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-8408
Radar None
Original Reporter prashantkt (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Swift4

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 9f6de4517e2d19194a20bbcc9baae45c

Issue Description:

Hi There Here is String extension

extension String {
        func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
            let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
            let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

            return ceil(boundingBox.height)
        }
}

While accessing

boundingRect

Property I got error Use of unresolved identifier 'boundingRect' if I use

 let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

Error disappears .

@swift-ci
Copy link
Contributor Author

Comment by Prashant Tukadiya (JIRA)

I have checked further and found that `boundingRect` is method of extension NSString. and if i change extension String to extension NSString i can access it without self. Any one can help me to figure it out. it is swift bug or I am doing something wrong ?

@belkadan
Copy link

Hm. There's an old feature that members of NSString are accessible on String through member access syntax, but I think it's not a recommended feature at this point anyway. It's better to explicitly convert to NSString using as. Still, though, it is confusing that using self is different from not.

cc @DougGregor, @rudkx

@swift-ci
Copy link
Contributor Author

Comment by Prashant Tukadiya (JIRA)

@belkadan Sorry I didn't get it that why I need to use self. I understand that if property / method of String it would be easily accessed without self, as it is NSString extension it is not directly accessible without self.
I think use of self that bridges Swift's String and NSString extension.

@belkadan
Copy link

The code in the compiler that says "NSString's members show up on String values" is specifically looking for member syntax, i.e. "foo.bar"—or in this case, "self.bar". It probably shouldn't have been written that way, but it was.

I think we'd still recommend being explicit here, even if it's ugly: (self as NSString).boundingRect(…) or let nsSelf = self as NSString; nsSelf.boundingRect(…).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants