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-957] make String implement CVarArg protocol to support formats with "%@" on Linux #4368

Closed
swift-ci opened this issue Mar 16, 2016 · 11 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-957
Radar None
Original Reporter vadim (JIRA User)
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 7
Component/s Foundation, Standard Library
Labels Improvement
Assignee Molanda (JIRA)
Priority Medium

md5: 71333aaf50f2a65e850f26689b127b98

Issue Description:

make String implement CVarArg to support formats with "%@" for strings, e.g. with the following method:

https://github.com/apple/swift/blob/97d8f50af4978e54289377a0bd205e71a34529a2/stdlib/public/SDK/Foundation/NSStringAPI.swift

/// Returns a string created by using a given format string as a
/// template into which the remaining argument values are substituted
/// according to the user's default locale.
@warn_unused_result
public static func localizedStringWithFormat(
format: String, _ arguments: CVarArg...
)

A format example: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html

Format specifiers:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

@belkadan
Copy link

@gparker42, @gribozavr, was there a reason we didn't do this? Was it just to avoid confusion with %s?

vadim (JIRA User), you can work around this by explicitly coercing to NSString or AnyObject using as.

@gribozavr
Copy link
Collaborator

We can't make AnyObject conform to CVarArg.

@belkadan
Copy link

Ah, right, of course. But that doesn't say why we wouldn't do it for String, and I think we do (or could) do it for NSObject, at least.

@swift-ci
Copy link
Contributor Author

Comment by Ian Partridge (JIRA)

let args: [CVarArg] = [NSString(string: "hello world")]

doesn't compile on Linux, because NSObject only conforms to CVarArg on Darwin. Is fixing that a better way to go?

@swift-ci
Copy link
Contributor Author

swift-ci commented Oct 3, 2016

Comment by Jason Ji (JIRA)

Hello, just wondering if there was any progress on this? I also ran into an issue today where a call to

@gwynne
Copy link
Contributor

gwynne commented Feb 21, 2017

Wanted to bump this, as I've run into it too.

@jckarter
Copy link
Member

It'd be nice if all the bridged types conformed to CVarArg even on Darwin to avoid the as CVarArg explicit bridging conversions too.

@swift-ci
Copy link
Contributor Author

swift-ci commented Jan 9, 2018

Comment by Matthew Burke (JIRA)

One more bump^Wdata point. Just ran into this as well.

@swift-ci
Copy link
Contributor Author

Comment by Joseph Lawson (JIRA)

A patch can be found here in the mean time:

IBM-Swift-Sunset/GRMustache.swift@56ecaf2

We ended up munging the data into the type desired with as! CVarArg:

return String(format: NSLocalizedString("Configuration Error: %1$@", comment: "The error message displayed for configuration errors. (1: configuration error details)"), details.localized() as! CVarArg)

@swift-ci
Copy link
Contributor Author

Comment by Brian Gontowski (JIRA)

I created PR #2821 and PR #32311 to implement the CVarArg protocol on NSString and String.

There is some discussion in the Swift forums about this.

@swift-ci
Copy link
Contributor Author

swift-ci commented Oct 3, 2020

Comment by Brian Gontowski (JIRA)

The pull requests have been merged.

@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

5 participants