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

Swift support #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

krunk4ever
Copy link

When attempting to use this framework with Swift 3.0, I kept on getting the following error:

let helloWorldText = NSAttributedString(string: "hello world")
NSAttributedString(format: "%@", helloWorldText)

Ambiguous use of 'init'

I had to modify the header following the same pattern that NSString.h uses in order my compilation to work. The pattern is that the pass in locale as a nullable id, instead of nullable NSLocale *

- (instancetype)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
- (instancetype)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);
- (instancetype)initWithFormat:(NSString *)format locale:(nullable id)locale, ... NS_FORMAT_FUNCTION(1,3);
- (instancetype)initWithFormat:(NSString *)format locale:(nullable id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION

I've also added nullability to your header file as well.

replace NSDictionary with id to avoid ambiguous init for Swift
@krunk4ever
Copy link
Author

krunk4ever commented Jul 31, 2017

Forgot to mention I added a Swift extension in my project:

extension NSAttributedString {

    /// Allows you to format NSAttributedString the same way as NSString
    /// by passing in a comma separated list of attributed strings after the format
    ///
    /// - Parameters:
    ///   - format: format
    ///   - arguments: comma separated list of NSAttributedString
    convenience init(format: String, _ arguments: CVarArg ...) {
        // this is how to convert an array of CVarArg to a CVaListPointer
        let argList = getVaList(arguments)
        self.init(format: format, arguments: argList)
    }

}

which allows me to called the sample code below:

let helloWorldText = NSAttributedString(string: "hello world")
NSAttributedString(format: "%@", helloWorldText)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant