-2
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)

6
  • 4
    did you read the top upvoted answer on that question? stackoverflow.com/a/42974269/3694524 StaticString can only be determined at compile time, therefore it can't be converted from a runtime String type. Commented Jun 28, 2022 at 1:03
  • Yes, it doesn't solve my problem. That's why I am saying it is not a duplicated question although they look like it. Commented Jun 28, 2022 at 1:06
  • 1
    If you can explain your problem further, we can try to address the source problem. As written, it's an exact duplicate. What you've asked for is impossible, by design, and the linked question answers that. If you'll give some more details of your specific problem, it might be solvable by getting rid of the need to convert a runtime string into a compile-time string (which cannot be done; it's what it means to be a StaticString). Commented Jun 28, 2022 at 2:21
  • @RobNapier Thx, I will update the description. Commented Jun 28, 2022 at 5:29
  • Off topic but since a function parameter is immutable unless declared as inout, is there any valid reason to use StaticString instead of String as a parameter type? Commented Jun 28, 2022 at 6:46

1 Answer 1

3

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!"
Sign up to request clarification or add additional context in comments.

3 Comments

I can't, it is dynamically generated, but the method I am calling has a StaticString parameter.
Not clear on your use case but perhaps can use code generation with a compile time build step to dynamically construct the string. If the string relies on runtime data, it's not going to be possible.
Depending what you're using to generate the relevant string, you might consider a pre-build shell script or something to generate the relevant source correctly. You can use string interpolation to create the StaticString, but everything that goes into it needs to be known at compile time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.