Hope you can help me, I am a bit of a newbie and have progressed without needing help but now I am stuck.
Firstly could somebody please tell me what this is below, I think it is ASCII but maybe wrong???
"\0\u{01}\0\0\0\u{1E}\0\0\u{02}\u{06}\0ÿ\0\0\0\0\0\u{02}.\u{01}(\0\t\0ü\0\u{07}\0\u{04}"
Secondly how would I convert this to an NSArray so I could get iterate through it and get a value at a given location in the array?
I am using Swift
Many thanks in advance
Charles
As mentioned above, your string looks quite weird, and the \u{xx} values tell Swift to replace that part with the unicode character corresponding to hexadecimal value xx (see unicode-table).
I do not know why you would want to convert it to the Objective-C type NSArray while you could just use the Swift type Array. By that, I mean you can generate an array of the string by using var strArray = Array(str), where your string is stored in str. From that point on, you can use the following code to iterate over the contents of the string:
for var char in strArray {
//do something with char
}
char then loops through all indices of the strArray, holding the value at the current index.
Related
I have a localized string that looks like this:
String(format: "unable_to_reach".localized(), name) //name is a string. I have also tried just "x"
The key/value pair in the localize file looks like this:
"unable_to_reach" = "Unable to reach %1$s";
Now, sometimes this works, othertimes it crashes with the EXC_BAD_ACCESS error. Why is this? isn't %1$s supposed to be used for string values?
The format specifier %1$s is %s with a positional specifier of $1 inserted into it. %s is the format specifier for a null-terminated C string. If you instead pass it a Swift String, bad things will happen. Don't do that. (It will likely cause a buffer overflow if the Swift string does not contain any null bytes.)
You want %# (or %$1#, to preserve the positional specifier.)
See the document on string format specifiers for more information.
Edit:
BTW, you should think about using Swift string interpolation instead:
let unableToReach = "unable_to_reach".localized()
let final = "\(unableToReach) \(name)"
That is more "Swifty".
Note that if you want to use localized placeholders to allow for different word ordering in different languages, you really still need to use String(format:) and the %# (or %1# positional syntax) version.
I have an app UI that expects a HEX value e.g. foo = 0x113
I'm doing this in Lua to try to write to foo:
menu.set("Presets", "foo", "0x318")
menu.set("Presets", "888x", "-258")
menu.set("Presets", "89ab", "-60"
The values for 888x and 89ab in the app are set. The HEX value field remains empty. Could someone help please? Thanks.
There is no such thing as an hex value. There are numbers expressed in hex.
So your API expects a number. No wonder "0x318" does not work. The other two work because the strings are convertible to numbers.
Bottom line: use menu.set("Presets", "foo", 0x318).
I have an issue in an application I'm writing where I need to compare one NSURL that points to a file and an NSString, which is an incoming string representation of the same file path.
I can't get them to compare – the output I'm given when NSLogging is confusing, perhaps it is a encoding issue?
I can make them look the same with this code: [urlString stringByRemovingPercentEncoding];
The raw output for the NSURL is:
file:///var/mobile/Applications/F14AFBD8-FF60-4094-8BBD-7AC2477E0B20/Documents/1.%20AKTIV%20SA%CC%88LJFOLDER/Sa%CC%88ljfolder2014-SP1.pdf
And for the NSString:
/var/mobile/Applications/F14AFBD8-FF60-4094-8BBD-7AC2477E0B20/Documents/1. AKTIV SÄLJFOLDER/Säljfolder2014-SP1.pdf
If I run stringByRemovingPercentEncoding on the NSURL it looks the same, but they don't compare.
If I run stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding to the NSString I get file:///var/mobile/Applications/F14AFBD8-FF60-4094-8BBD-7AC2477E0B20/Documents/nestle/1.%20AKTIV%20S%C3%84LJFOLDER/S%C3%A4ljfolder2014-SP1.pdf
Note that the percentages is not the same on the urls. I have tried so many things, changing encodings etc. but can't find a way to solve this.
Edit
So, I tried the precomposedStringWithCanonicalMapping as follows:
NSLog(#"EQUAL? :%hhd", [[strippedUrlString precomposedStringWithCanonicalMapping] isEqualToString:[filePath precomposedStringWithCanonicalMapping]]); – returns 0
I logged the strings and got
/Users/xxxxxx/Library/Application Support/iPhone Simulator/7.0/Applications/C05E0885-7B58-4B2F-A6B4-D9388E60462C/Documents/1. AKTIV SÄLJFOLDER/Säljfolder2014-SP1.pdf
with NSLog(#"Precompose url 1: %#", [strippedUrlString precomposedStringWithCanonicalMapping]);
for the first string and
/Users/xxxxxx/Library/Application%20Support/iPhone%20Simulator/7.0/Applications/C05E0885-7B58-4B2F-A6B4-D9388E60462C/Documents/1.%20AKTIV%20SA%CC%88LJFOLDER/Sa%CC%88ljfolder2014-SP1.pdf
with NSLog(#"Precompose file 1: %#", [filePath precomposedStringWithCanonicalMapping]);
for the second.
Tried same code, but with precomposedStringWithCompatibilityMapping and got exactly the same result :(
Probably you ran in a problem that in Unicode equivalent strings are not always binary equal.
http://en.wikipedia.org/wiki/Unicode_equivalence
You have
…SA%CC%88…:
This is the problem.
It means: We have an "A" and a combining diaeresis -> Ä. The diaeresis is the 0xCC88, which is UTF-8 for Unicode 0x0308 (COMBINING DIAERESIS). So the Ä is encoded as an A with an combining diaeresis.
…S%C3%84…:
This is easy. 0xC384 is UTF-8 for 0x00C4 that means A-Umlaut -> Ä
First of all: What is the source of the first string?
Addition: You can use precomposedStringWith…Mapping (NSString).
BTW: You can compare strings without diacritic marks using -compare:withOptions: et al. with the option NSDiacriticInsensitiveSearch. In this case, I assume, string 1 equals string 2. Butt it would equal an "A", too, what is probably not what you want.
I have a string such as "abbaaxaa" and I want the characters in the string to be converted into a set such that its elements are "a", "b", and "x"? (I'm a noob coder and I just thought of this implementation to represent the keys for a dictionary, if context matters at all).
I also have code on what I've attempted so far, but it has errors (i.e. where initializing the set and syntax of expressions are concerned) and I am not sure how to go about implementing it... I just know I'm working inside a class (+) method that returns an NSSet.
Use an NSMutableSet. Loop through the string character by character, implement the loop body to read something like
[set addObject:[str substringWithRange:NSMakeRange(idx, 1)]]
I have a text file which read:
config<001>25
23<220>12
.....
how can i parse so that i need only the values config,001(to be converted into integer after extracting using strtok or any ohter methods please suggest), and 25(to be converted into integer) seperately. i tries strtok its not working as the way i need. Please help me.
Use LINQ 2 SQL to import the file on the delimiters and then use something like AutoMapper to do the mapping of fields to say specific objects with specific types.
I did this exact thing in another project and it works great.
Based on the mention of strtok I'm guessing that you're using C or C++. If you're using C++, I'd probably handle this by creating a ctype facet that treats < and > as white space, which will make the parsing trivial (infile >> string >> number1 >> number2;).
If you're using C, you can use the scan-set conversion with scanf, something like: sscanf(line, "%[^<] %d> %d", string, &number1, &number2);