I have a function which add classical emoji to a string
var emojiList: [String] = []
func initEmoji() {
for c in 0x1F601...0x1F64F{
self.emojiList.append(String(describing: UnicodeScalar(c)!))
}
}
I would like to use other pod with Emoji, like Emoji-Swift or SwiftEmoji
I don't know how to append my emojiList array with all those new Emoji.
Could someone kindly tell me how to do ?
It might be easy, but I'm stuck ..
Thanks !
The solution (swift 3)!
var emojiDictionary = String.emojiDictionary
for (myKey,myValue) in emojiDictionary {
self.emojiList.append(myValue)
Related
I've been going through Glassdoors for companies I am interested in interviewing with and saw one that said the company asked them a question about "appending a name onto a list". I have been trying to figure out how to go about that. I know the function will take an array and will return an array as well. Can someone help me write that out and explain it to me? Attached is my version, which does not work. It just prints out the existing string twice
func addNameToList(_ names: [String]) -> [String] {
var names = ["Ben", "Sam", "Ken"]
var results = [names[0]]
for name in names {
names.append(name)
print(names)
}
return results
}
addNameToList([String("Louis")])
if you are need to add one or more string value with Existing string array you can use like below
var globalArrayString = ["One","two","three"]
func appendString(_ currentString: String...) -> [String] {
globalArrayString.append(contentsOf: currentString)
return globalArrayString
}
// if you need to add more String,Call like below
let combinedArray = appendString("four","five")
print(combinedArray)
I'm rushing a project and am not able to research to find the answer. I hope I can get some help on this.
var tt: Array<[String:Any]> = []
var forpickarray: [String] = []
Error: cannot convert [String:Any?] to [String]
for index in 1...self.tt.count {
var arr: [String] = self.tt[index-1]
forpickarray.append(self.tt[index-1])
}
I am trying to get the value- Just the listname
[["total": 2, "listName": Testing, "listid": 1], ["total": 1, "listName": Yeeea, "listid": 2]]
Oh, just seen your edit. Helps a lot.
To get the value of the listName from each dictionary you can do like this...
forPickArray = tt.flatMap { $0["listName"] }
Rename your variables to have sensible names. tt is not a sensible name. I have no idea what it is :D
within your for-loop, are you trying to extract keys from the dictionary and convert it to an array? then you need to do the following:
var arr: [String] = Array(tt[index-1].keys)
forpickarray.append(contentsOf: arr)
Because tt is an array of dictionary, each element in tt could have many keys
Please use native Swift syntax rather than ugly Objective-cish code.
A loop is not needed at all:
forpickarray = tt.flatMap{ $0["listName"] as? String }
Even if a loop was required there is a much more efficient way than a (zero!)index-based for loop:
for item in tt {
forpickarray.append(item)
}
I'm getting the error message Argument labels '(of:)' do not match any available overloads. Below is the code I'm using.
let prefs = UserDefaults.standard
var id: String!
if var array = prefs.string(forKey: "myArray"){
if let index = array.index(of: id) {
array.remove(at: index)
prefs.setValue(array, forKey: "myArray")
}
}
I've seen a lot of answers on Stack Overflow with very similar code to that. So I'm not quite sure why this wouldn't be working.
Basically I'm just trying to remove the element in the array that = id then set that new array to the user defaults.
Update
Just updated the code above to show how array is getting defined. id is a string that is defined in a separate section.
By accessing prefs.string(forKey: "myArray"), you are getting a String, not an array of strings. You should use this:
if var prefs.array(forKey: "myArray") as? [String] { }
or
if var prefs.value(forKey: "myArray") as? [String] { }
Make sure to not forget putting as! [String], because the first method returns [Any], an which can contain objects of any type, not specifically String. Then your error should be solved, because index(of: ) can only be used on Arrays of specified types.
Hope it helps!
Just make an alt + Click on an "array" variable to make sure it is of type Array ([String]), not a String. To apply .index(of:) method it must be an array.
Like this:
String does not have a method .index(of:). That's what the error is pointing at. And sure make a cast to [String]? if it fits.
In my app I have two struct arrays and I want to remove common items from one of them. My struct:
struct PeopleSelectItem {
var name = ""
var id = ""
var added = false
}
My arrays:
var people : [PeopleSelectItem] = []
var selectedPeople : [PeopleSelectItem] = []
I want to remove items from people array if they exist (compare by id) on selectedPeople array.
I tried several array filtering and converting to set but none of them worked. What can I do here?
Thanks!
Get an array of all ids in selectedPeople
let selectedPeopleIDs = selectedPeople.map(\.id)
Filter the items whose id is not in the array
let filteredPeople = people.filter { !selectedPeopleIDs.contains($0.id) }
If you know that people equal each other if the id is the same then you can conform your struct to the Equatable protocol and you can use the array filter method.
struct PeopleSelectItem : Equatable {
var name = ""
var id = ""
var added = false
}
func ==(lhs: PeopleSelectItem, rhs: PeopleSelectItem) -> Bool {
return lhs.id == rhs.id
}
func filterPeople() {
//swift 2, 3:
people = people.filter{!selectedPeople.contains($0)}
//swift older versions:
people = people.filter{!contains(selectedPeople, $0)}
}
If people might have a significant amount of entries, performance should be considered. So, instead of searching with an n^2 algorithm, you should make use of Swifts dictionary and the corresponding hash-search to find items.
If Id is unique for people then I would store them in a dictionary like:
var peopleDict: [String:PeopleSelectItem] = [:]()
You can easily convert from the array you have to this dictionary:
people.foreach {peopleDict[$0.id] = $0}
With this dictionary it's very easy to delete single entries:
selectedPeople.foreach {peopleDict.remove($0.id)}
Optionally to switch back to an array for people you just say:
let filteredPeople = peopleDict.values as [PeopleSelectItem]
Remarks
I assumed, that selectedPeople is smaller then the base of all people. If this is not the case, you should pu selectedPeople in a dictionary.
did I say I like this Spark like api? I think I do so.
I just wrote that code from top of my mind. If it is not completely syntactically correct let me know and I correct it.
I want a simple function that can remove part of a string, eg:
var foo="oranges";
trace(removeStrings(foo,'rang'));
I want the above output as 'oes'. Any help will be greatly appreciated.
Thanks in advance
A quick solution for removing substrings is to use split with the string that you want to remove as delimiter and then join the result:
function removeSubString(str, remove):String {
return str.split(remove).join("");
}
Another way to do this is
function removeStrings(originalString, pattern):String
{
return originalString.replace(pattern, "");
}
For more information about Strings in AS3 you can visit:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html
I should mention that the code above is not going to change your String, so if you need to use the property originalString with the new value you should use:
originalString = removeStrings(originalString, pattern);
The second thing that I should mention is that the replace method will replace the first appearance of the pattern, so if you need to replace every match of the pattern you should do something like
while(originalString.search(pattern) != -1)
{
originalString = removeStrings(originalString, pattern);
}
Hope this will help!
Ivan Marinov
I'm using by long time this snippet, which as the advantage to be available to all string objects on your movie:
String.prototype.replace = function(pattern, replacement) {
return this.split(pattern).join(replacement);
}
can be used in this way:
var str = "hello world";
var newstr = str.replace("world", "abc");
trace(newstr);
as you can see the string class have been extended with the replace method.