简体   繁体   中英

iOS Swift: How to convert String to StaticString?

func generateDescription(_ prefix: String) {
    return (prefix + " Some Text Here")
}

let str: String = generateDescription("Some prefix text here")

How do I cast or generate a StaticString from this runtime generated string? I need to pass str to a method from a library that has a StaticString parameter (I have no control over the library).

I am thinking of something like this:

let staticStr = StaticString(str)

But this is not the correct way.

Thanks!

(Btw this is not a duplicate to this question: Convert String to StaticString )

I'm afraid it is a duplicate of Convert String to StaticString - there isn't a mechanism to convert to a static string during the execution of your program, as the whole purpose of static string is to have a string that's fully defined at compile time.

If you want to use a static string, you need to define it entirely up front in your code as a StaticString:

let staticString = "Hello, World!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM