I have an array containing string type values, and one value of a string contains the symbol & and another the symbol ^. So when it's time for them to be shown the UILabel remains blank.
let myString = arrayStrings[0] // The value is "M&M" or "(0C)^3"
myLabel.text = myString //UILabel remains blank
On the other hand, when I hardcode the string, the UILabel displays it.
myLabel.text = "M&M" //UILabel displays it normally
What can I do?
Just realised that when I print the Array I have the following result:
print("Array: \(arrayStrings)" // Array: ["\0M&M\0", "\0(0C)^3\0"]
"\0" doesn't exist to the rest Strings of the array
Thank you!
\0 means string termination in programming. So your String "\0M&M\0" means that this string is terminated on index 0, and then again later. So when you assign this string to your label, your label is displaying empty string.
To tweak it, do this and you will see the difference. Your problem is not related to & or ^
myLabel.text = "M&M\0 Hey I have lots of stuff here but the string is already terminated"
So in this situation, you need to find out why \0 exists in your string. You can possibly remove them by regex or string replace.
Related
I have to delete an entry from a UILabel. I am doing so by copying its text to a string variable and deleting the end index to the string. The UILabel does contain a value but the string it is being copied to contains nothing. Any help would be most appreciated.
var displayString = display.text!
displayString.removeAtIndex(displayString.endIndex) //error at this line.
display.text! = displayString
The state of the string can be seen in this screenshot.
and the debugger can be seen here that the display UILabel actually does contain the string.
You should not remove the endIndex, but the one before the end. And you should only remove something from the string if there actually is something to remove - check for the empty string before removing a character:
if displayString != "" {
displayString.removeAtIndex(displayString.endIndex.predecessor())
}
So, I have a cell with one label inside. I am trying to populate that label text with the various items in my array - all strings.
My array
var restauranttypelist: [String] = ["American", "Asian", "Bakery & Deli",
"Burgers", "Italian", "Mexican", "Seafood", "Steakhouse"]
and my cell label text
let type = restauranttypelist [indexPath.row]
var typecell = tableView.dequeueReusableCellWithIdentifier("cellone") as RestaurantTypeCell
typecell.restaurantTypeLabel.text = restauranttypelist.text
return typecell
I have tried a number of solution ranging from ".text" seen above, to ".String", to "restauranttypelist:indexPath.row" to no avail.
I suppose I have two questions. Am I setting up my array correctly? Do I need to insert the "[String]" portion after the variable name?
Finally, how would I be able to set the cell label to the numerous items I have in my array?
Thanks for any help... beginning.
Jon
In let type = restauranttypelist[indexPath.row] you're accessing a String from your Array and storing it in type. Therefore, all you need is typecell.restaurantTypeLabel.text = type.
There's nothing wrong with how you setup the array. You don't need the [String] type annotation since it can be inferred from the value you are assigning to it, but having it there does no harm.
Finally, this doesn't affect how your code works, but it's nice to know anyway:
Swift variable names follow the convention of starting with a lowercase character, and then capitalizing every subsequent word. Following that convention your variable names should be typeCell and restaurantTypeList.
Characters as (é) or in arabic (دٌ) are counted as one in a string, how do I make it recognise the mark as a character?
It should be like (د) is a character and (ٌ) is another character.
I don't want to use NSString because I'm using (startIndex) which is not supported in NSString as far as I know.
Thank you
I’m by no means sufficiently knowledgeable in this area to be confident there aren’t some gotchas from this approach, but this appears to do what you’re looking for:
let s = "éدٌ"
let separated = map(s.unicodeScalars) { Character($0) }
println(" , ".join(separated.map(toString)))
// prints "e , ́ , د , ٌ"
Note, if you create a new string from a sequence of those separated characters, it will recompose them:
println(String(separated)) // prints
// prints "éدٌ"
In a UITableView, I'm listing a bunch of languages to be selected. And to put a section index view to the right like in Contacts app, I'm getting all first letters of languages in the list and then use it to generate the section index view.
It works almost perfect, Just I encountered with a problem in getting first letter of some strings in Hebrew. Here a screenshot from playground, one of the language name that I couldn't get the first letter:
Problem is, the first letter of the name of the language that has "ina" language code, isn't "א", it's an empty character; it's not a space, it's just an empty character. As you can see, it's actually 12 characters in total, but when I get count of it, it says 13 characters because there is an non-space empty character in index 0.
It works perfectly if I use "eng" or "ara" languages with putting these values in value: parameter. So maybe the problem is cause of system that returns a language name with an empty character in some cases, I don't know.
I tried some different methods of getting first letter, but any of it didn't work.
Here "א" isn't the first letter, it's the second letter. So I thought maybe I can find a simple hack with that, but I want to try solving it before trying workarounds.
Here is the code:
let locale = NSLocale(localeIdentifier: "he")
let languageName = locale.displayNameForKey(NSLocaleIdentifier, value: "ina")!
let firstLetter = first(languageName)!
println(countElements(languageName))
for character in languageName {
println(character)
}
You could use an NSCharacterSet.controlCharacterSet() to test each character. I can't figure out how to stay in Swift-native strings, but here's a function that uses NSString to return the first non-control character:
func firstNonControlCharacter(str: NSString) -> String? {
let controlChars = NSCharacterSet.controlCharacterSet()
for i in 0..<str.length {
if !controlChars.characterIsMember(str.characterAtIndex(i)) {
return str.substringWithRange(NSRange(location: i, length: 1))
}
}
return nil
}
let locale = NSLocale(localeIdentifier: "he")
let languageName = locale.displayNameForKey(NSLocaleIdentifier, value: "ina")!
let firstChar = firstNonControlCharacter(languageName) // Optional("א")
I want my label to read like this:
Name of Activity
nn%
Instead, here's what appears:
%#
%f%
In addition, I'm getting this warning: Expression result unused
Here's the code I'm trying:
firstLabel.text = #"%#\n%#%",[self.thisSpec activityOfInterest],focusActivityPercent;
[self.thisSpec activityOfInterest] returns a string containing the name of an activity, and focusActivityPercent is a double.
This is the first time I've tried a multiline label.
Any suggestions?
Thanks
You can't specify string formatting on a string literal on its own like that. In fact, the code you've shown should be producing a syntax error. You have to use NSString's stringWithFormat: class method:
firstLabel.text = [NSString stringWithFormat:#"%#\n%#%",[self.thisSpec activityOfInterest],focusActivityPercent];