2

so I have the following link in my Localizable.strings file:

someURL = "https://www.thisisthelink.be/api/article?categoryId=%@&key=ehhssd%bfbfs43-53456Gdffd~563&lang=nl&amount=1000&mobile=true";

The part where there is the %@ I need to fill in the ID from that category (for example 27). So further in my code I do the following

   let URL = String(format: "someURL".localized,String(id))

But this gives a crash, And I don't really know why (the crash is just a Thread 1: EXC_BREAKPOINT(code=1,...) so I can't deduct what exactly is wrong).

The .localized is the following extension

extension String {
    var localized: String {
        return NSLocalizedString(self, comment: "")
    }
}
SoundShock
  • 485
  • 1
  • 3
  • 24
  • `But this gives a crash` What's the error message? Where does it appear? – Eric Aya May 30 '16 at 08:36
  • @EricD the crash is just a Thread 1: EXC_BREAKPOINT(code=1,... . It appears at the `let URL` line . The error message is super generic, and doesn't even show up in the output. – SoundShock May 30 '16 at 08:37
  • Does `print("someURL".localized)` output what's expected? Looks like there's an issue with the localized string. – Eric Aya May 30 '16 at 08:40
  • @EricD When i just do a `print(someURL.localized)` it seems to work, gives the following output: `https://www. thisisthelink.be/api/article?id=%@&key=ehhssdbfbfs4353456Gdffd4563&lang=nl&amount=1000&mobile=true` – SoundShock May 30 '16 at 08:41

2 Answers2

0

I suppose there isn't an issue about the special characters but you can try to do :

let specialChars = "%@"
someURL = "https://www.thisisthelink.be/api/article?categoryId=\(specialChars)&key=ehhssdbfbfs4353456Gdffd4563&lang=nl&amount=1000&mobile=true"
let uRL = String(format: NSLocalizedString(someURL, comment: "someURL"), String(id))

I dont like to localize url's because they can have unpredictable compositions, the best way it's to localize only the text to avoid precisely the format problems.

P.S.: Usually using uppercase to the properties name it's considered as a bad attitude, you should use uRLstr or uRL instead of URL

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • The "special character" is the formatter for the String. Declaring it outside the string and then inserting it - before using String(format:) - is not going to change anything... – Eric Aya May 30 '16 at 08:55
  • It's not correct. When you work with string interpolation and you use formats where your string contains another percent character the compiler can be crash , you cannot know what parameters can be have this url so the best way it's to separate it – Alessandro Ornano May 30 '16 at 09:06
  • Tried your way, but it didn't make a difference, same issue. You are right about one thing, my key contains the following special characters: %~ and - . I'll add it in the link – SoundShock May 30 '16 at 09:13
  • 1
    Also, the someURL comes from the Localizable.strings file, so the \() can't work, right? That's why I use the %@ sign – SoundShock May 30 '16 at 09:21
0

I don't understand why you want a request like that. In my way, I use base URL+interface name+ params instead. Like using AFNetworking, params should be a dictionary that is easy to generate.

Anyway, if you want a long-string-like URL, in Swift you'd better use "categoryId=\(id)" to load different id you want.

And finally, if you insist using that:

String(format: someURL, arguments: ["123"])

Klein Mioke
  • 1,261
  • 2
  • 14
  • 23
  • can you give an example of your way? I'm using AlamoFire as well – SoundShock May 30 '16 at 10:59
  • `Manager.sharedInstance.request(method, api.apiURLString(), parameters: params, encoding: .URL, headers: nil)` params is [String: AnyObject]. `api` is APIManager, an object class provide some information about the API(like full URL string). – Klein Mioke May 31 '16 at 02:56