Swift -sort array by substring - ios

I have an array of strings (they were urls that are casted to strings), in the middle of each string they all have a UUID().uuidString with a count variable and a .jpg in them.
let arr = [ htps://firebasestorage.googleapis.com/myApp.appspot.com/o/users%Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg?alt=media&token=e215e6a1-f5b9-431e-83a3,
htps://firebasestorage.googleapis.com/myAapp.appspot.com/o/users%-Ll_Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg?alt=media&token=f350cf36-4c4e-4faf,
htps://firebasestorage.googleapis.com/myAPp.appspot.com/o/users%mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg?alt=media&token=123uyqtr
....]
The first element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg
The second element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg
The third element has this in the middle of it: 2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg
The fourth element and on and on ..
How can I sort these strings in this array based on either the substring of the UUID with the _x.jpg or just the _x.jpg alone?
FYI I have access to the UUID beforehand

You can sort the array this way
Convert the strings (back) to URL.
Get the lastPathComponent of each URL.
extract the substring from the last underscore character to the end.
Compare the strings with compare: and numeric option or localizedStandardCompare:

If your starting array is something like this
let array = ["htps://firebasestorage.googleapis.com/myApp.appspot.com/o/users%Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_3.jpg?alt=media&token=e215e6a1-f5b9-431e-83a3","htps://firebasestorage.googleapis.com/myAapp.appspot.com/o/users%-Ll_Mxd6EUO5l2LK-mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_1.jpg?alt=media&token=f350cf36-4c4e-4faf","htps://firebasestorage.googleapis.com/myAPp.appspot.com/o/users%mKa%2F4606E275-B2C5-4A69-B997-01423ABFE3B7%2FBE26726D-B8E5-47C8-9A18-504D23B99090_2.jpg?alt=media&token=123uyqtr"]
Give this one a try.
You can split based on your jpg suffix, and then based on your UUID.
let sortedArray = array.sorted { (first, second) -> Bool in
let firstIndex = Int((first.components(separatedBy: ".jpg")[0]).components(separatedBy: "FBE26726D-B8E5-47C8-9A18-504D23B99090_")[1]) ?? -1
let secondIndex = Int((second.components(separatedBy: ".jpg")[0]).components(separatedBy: "FBE26726D-B8E5-47C8-9A18-504D23B99090_")[1]) ?? -1
return firstIndex < secondIndex
}

Related

How to find first non-zero element of an Array?

Is there a way to grab the first non-zero element from an array of numbers?
I have an Array with many zeros at the beginning and I need only the first item that is not a zero.
For example:
let array = [0,0,0,0,25,53,21,77]
based on the above, the result should be 25.
What is the good way to achieve it?
You could get it like this:
let array = [0,0,0,0,25,53,21,77]
let firstNonZero = array.first { element -> Bool in
return element != 0
}
Or as a shorter version:
let firstNonZero = array.first(where: { $0 != 0 })
Note that firstNonZero would be an optional Int, so in case of array contains only zeros, firstNonZero would be nil.
Aside bar note: If you were wondering why to use first(where:) instead of filter(_:).first, you could check this question:
What is the difference between filter(_:).first and first(where:)?

How to get substring from user input?

i wrote code to get character when user enter in text field and do math with them
this :
#IBOutlet weak internal var textMeli: UITextField!
var myChar = textMeli.text
var numb = [myChar[0]*3 , myChar[1]*7]
but one is wrong
textMeli.text is a String.
myChar is a String.
You can't access a Character from a String using bracket notation.
Take a look at the documentation for the String structure.
You'll see that you can access the string's characters through the characters property. This will return a collection of Characters. Initalize a new array with the collection and you can then use bracket notation.
let string = "Foo"
let character = Array(string.characters)[0]
character will be of type Character.
You'll then need to convert the Character to some sort of number type (Float, Int, Double, etc.) to use multiplication.
Type is important in programming. Make sure you are keeping track so you know what function and properties you can use.
Off the soap box. It looks like your trying to take a string and convert it into a number. I would skip the steps of using characters. Have two text fields, one to accept the first number (as a String) and the other to accept the second number (as a String). Use a number formatter to convert your string to a number. A number formatter will return you an NSNumber. Checking out the documentation and you'll see that you can "convert" the NSNumber to any number type you want. Then you can use multiplication.
Something like this:
let firstNumberTextField: UITextField!
let secondNumberTextField: UITextField!
let numberFormatter = NumberFormatter()
let firstNumber = numberFormatter.number(from: firstNumberTextField.text!)
let secondNumber = numberFormatter.number(from: secondNumberTextField.text!)
let firstInt = firstNumber.integerValue //or whatever type of number you need
let secondInt = secondNumber.integerValue
let product = firstInt * secondInt
Dealing with Swift strings is kind of tricky because of the way they deal with Unicode and "grapheme clusters". You can't index into String objects using array syntax like that.
Swift also doesn't treat characters as interchangeable with 8 bit ints like C does, so you can't do math on characters like you're trying to do. You have to take a String and cast it to an Int type.
You could create an extension to the String class that WOULD let you use integer subscripts of strings:
extension String {
subscript (index: Int) -> String {
let first = self.startIndex
let startIndex = self.index(first, offsetBy: index)
let nextIndex = self.index(first, offsetBy: index + 1)
return self[startIndex ..< nextIndex]
}
}
And then:
let inputString = textMeli.text
let firstVal = Int(inputString[0])
let secondVal = Int(inputString[2])
and
let result = firstVal * 3 + secondVal * 7
Note that the subscript extension above is inefficient and would be a bad way to do any sort of "heavy lifting" string parsing. Each use of square bracket indexing has as bad as O(n) performance, meaning that traversing an entire string would give nearly O(n^2) performance, which is very bad.
The code above also lacks range checking or error handling. It will crash if you pass it a subscript out of range.
Note that its very strange to take multiple characters as input, then do math on the individual characters as if they are separate values. This seems like really bad user interface.
Why don't you step back from the details and tell us what you are trying to do at a higher level?

How to replace specific strings to NSTextAttachment for show image

I will get a lot of message string
Like these:
1.hello {{http://i.imgur.com/f1cqT3ut.jpg}} world {{http://i.imgur.com/f1cqT3ut.jpg}}
2.hi {{http://i.imgur.com/iVx9iqjt.jpg}} {{http://i.imgur.com/iVx9iqjt.jpg}} how {{http://i.imgur.com/ZpXgxiXt.jpg}} are {{http://i.imgur.com/rcdHObKt.jpg}} you {{http://i.imgur.com/yX5dHdet.jpg}} ? {{http://i.imgur.com/2iZSBKGt.jpg}}
And I want to handle these messages
Like these:
1.message handled 1
2.message handled 2
Now I only know can use NSMutableAttributedString to show all handled messages and use NSTextAttachment to show the url images.
But I don't know how to replace and handle these messages.
Help me, please.
Thanks.
If you can assume that each string will be setup like that, where the words and URL's are separated by spaces - and the URL's are all contained within two sets of curly brackets, you could create a method that would then take a string and split it into an array of substrings (separated by spaces), you could then check for a substring that starts with "{{" to know it's a URL. I just wrote something like this which would return an array of tuples where each tuple is an integer (correlating to the index of the URL within the array of substrings), and a string which represents the URL removed from the curly brackets.
func split(_ s: String) -> [(Int, String)] {
// Separate the original string into an array of substrings separated by a space
let separated = s.components(separatedBy: " ")
// Filter the separated array to find the URLs that start with {{
var urls = separated.filter{ $0.hasPrefix("{{") }
// Map the urls array to get an array of ints correlating to their respective indices from the separated array
let indicies = urls.map { separated.index(of: $0) }
// create an empty array of Int and String tuples
var tuples: [(Int, String)] = []
// Loop through the url array
for i in 0 ..< urls .count {
// Remove the left side (open) curly brackets
urls[i] = urls[i].replacingOccurrences(of: "{{", with: "")
// Remove the right side (close) curly brackets
urls[i] = urls[i].replacingOccurrences(of: "}}", with: "")
// Append the tuple with the url's index from the separated array and it's actual url value as a string
tuples.append((indicies[i]!, urls [i]))
}
return tuples
}
You could then use this to split the words from the urls, and then fetch the images (that would be up to you - maybe use Alamofire or even just NSURLSession) and then since you have their original index from the array of substrings - you still know what order they go in. So re-arrange them based on original order once the asynchronous request comes back, and then use UILabels sandwiched in between UIImageViews (or vice-versa) to display the content in the way you're trying.
let fullString = NSMutableAttributedString(string : "start of text")
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named : "image name")
let image1String = NSAttributedString(attachment: image1Attachment)
fullString.append(image1String)
fullString.append(NSAttributedString(string: "End of text"))
yourLabel.attributedText = fullString
This code is to shows a way that you want.Use for reference implement code as you need.

Cannot find char at position and index of char of a string var in Swift 2

Equivalent functions in Swift 2 of Java's charAt() and indexOf() ?
Firstly read this article about Swift strings and think about exactly what you mean by characters.
You can use the character view (or the utf16 view if that is the sort of characters that you want) of the string to see it as a collection and if you really need to get characters by index (rather than by iteration) you may want to convert it to an array but normally you just need to advance the index.
let myString = "Hello, Stack overflow"
// Note this index is not an integer but an index into a character view (String.CharacterView.Index)
let index = myString.characters.indexOf( "," )
let character = myString[myString.startIndex.advancedBy(4)] // "o"
This is O(n) (where n is the number of characters into the String) as it needs to iterate over the array as Characters may vary in length in the encoding)
Old answer below. The character array may be quicker for repeat access still as the array accesses are O(1) following the one off O(n) conversion to array (n is the array length).
let cIndex = 5
// This initialises a new array from the characters collection
let characters = [Character](myString.characters)
if cIndex < characters.count {
let character = characters[cIndex]
// Use the character here
}
Obviously some simplification is possible if the index is guaranteed to be within the length of the characters but I prefer to demonstrate with some safety on SO.
You can extend the String class with the missing charAt(index: Int) function:
extension String {
func charAt(index: Int) -> Character {
return [Character](characters)[index]
}
}
You have to convert the input string to an Array then return the character from the specific index here is the code
extension String {
func charAt(_ index : Int) -> Character {
let arr = Array(self.characters);
return (arr[index]);
}
}
This will work same as Java's charAt() method you can use it as
var str:String = "Alex"
print(str.charAt(1))
Did you read the NSString documentation? String and NSString are not identical, but as mentioned by this answer most functions are more or less the same. Anyway, the two exact functions you ask for are right there:
Java's charAt:
func characterAtIndex(_ index: Int) -> unichar
Java's indexOf:
func rangeOfString(_ searchString: String) -> NSRange
The NSRange data type contains just 2 variables: location and length that tell you where the substring is in the original string and how long it is. See the documentation.
//Java
"abc".indexOf("b") => 1
//Swift
"abc".rangeOfString("b").location #=> 1
you can used it instead of CharAt() in swift 3:
func charAt(str: String , int :Int)->Character{
str[str.startIndex]
let index = str.index(str.startIndex, offsetBy: int)
return str[index]
}

Search Array of Dictionaries for Value in Swift

I'm new to Swift and taking a class to learn iOS programming. I find myself stumped on how to search in an array of dictionaries for a string value and dump the string value into an array. This is taken from my Xcode playground.
I'm trying to figure out how to:
1) search through an array of dictionaries
2) dump the results of the search into an array (which I've created)
These are the character dictionaries.
let worf = [
"name": "Worf",
"rank": "lieutenant",
"information": "son of Mogh, slayer of Gowron",
"favorite drink": "prune juice",
"quote" : "Today is a good day to die."]
let picard = [
"name": "Jean-Luc Picard",
"rank": "captain",
"information": "Captain of the USS Enterprise",
"favorite drink": "tea, Earl Grey, hot"]
This is an array of the character dictionaries listed above.
let characters = [worf, picard]
This is the function I'm trying to write.
func favoriteDrinksArrayForCharacters(characters:Array<Dictionary<String, String>>) -> Array<String> {
// create an array of Strings to dump in favorite drink strings
var favoriteDrinkArray = [String]()
for character in characters {
// look up favorite drink
// add favorite drink to favoriteDrinkArray
}
return favoriteDrinkArray
}
let favoriteDrinks = favoriteDrinksArrayForCharacters(characters)
favoriteDrinks
I would be grateful for any assistance on how to move forward on this. I've dug around for examples, but I'm coming up short finding one that's applicable to what I'm trying to do here.
Inside the loop, you need to fetch the "favorite drink" entry from the dictionary, and append it to the array:
for character in characters {
if let drink = character["favorite drink"] {
favoriteDrinkArray.append(drink)
}
}
Note, the if let drink = guards against the possibility there is no such entry in the array – if there isn't, you get a nil back, and the if is checking for that, only adding the entry if it's not nil.
You might sometimes see people skip the if let part and instead just write let drink = character["favorite drink"]!, with an exclamation mark on the end. Do not do this. This is known as "force unwrapping" an optional, and if there is ever not a valid value returned from the dictionary, your program will crash.
The behavior with the first example is, if there is no drink you don't add it to the array. But this might not be what you want since you may be expecting a 1-to-1 correspondence between entries in the character array and entries in the drinks array.
If that's the case, and you perhaps want an empty string, you could do this instead:
func favoriteDrinksArrayForCharacters(characters: [[String:String]]) -> [String] {
return characters.map { character in
character["favorite drink"] ?? ""
}
}
The .map means: run through every entry in characters, and put the result of running this expression in a new array (which you then return).
The ?? means: if you get back a nil from the left-hand side, replace it with the value on the right-hand side.
Airspeed Velocity's answer is very comprehensive and provides a solution that works. A more compact way of achieving the same result is using the filter and map methods of swift arrays:
func favoriteDrinksArrayForCharacters(characters:Array<Dictionary<String, String>>) -> Array<String> {
// create an array of Strings to dump in favorite drink strings
return characters.filter { $0["favorite drink"] != nil }.map { $0["favorite drink"]! }
}
The filter takes a closure returning a boolean, which states whether an element must be included or not - in our case, it checks for the existence of an element for key "favorite drink". This method returns the array of dictionaries satisfying that condition.
The second step uses the map method to transform each dictionary into the value corresponding to the "favorite drink" key - taking into account that a dictionary lookup always returns an optional (to account for missing key), and that the filter has already excluded all dictionaries not having a value for that key, it's safe to apply the forced unwrapping operator ! to return a non optional string.
The combined result is an array of strings - copied from my playground:
["prune juice", "tea, Earl Grey, hot"]
let drinks = characters.map({$0["favorite drink"]}) // [Optional("prune juice"), Optional("tea, Earl Grey, hot")]
or
let drinks = characters.filter({$0["favorite drink"] != nil}).map({$0["favorite drink"]!}) // [prune juice, tea, Earl Grey, hot]
It may help you
var customerNameDict = ["firstName":"karthi","LastName":"alagu","MiddleName":"prabhu"];
var clientNameDict = ["firstName":"Selva","LastName":"kumar","MiddleName":"m"];
var employeeNameDict = ["firstName":"karthi","LastName":"prabhu","MiddleName":"kp"];
var attributeValue = "karthi";
var arrNames:Array = [customerNameDict,clientNameDict,employeeNameDict];
var namePredicate = NSPredicate(format: "firstName like %#",attributeValue);
let filteredArray = arrNames.filter { namePredicate.evaluateWithObject($0) };
println("names = ,\(filteredArray)");
Use the following code to search from NSArray of dictionaries whose keys are ID and Name.
var txtVal:NSString
let path = NSBundle.mainBundle().pathForResource(plistName, ofType: "plist")
var list = NSArray(contentsOfFile: path!) as [[String:String]]
var namePredicate = NSPredicate(format: "ID like %#", String(forId));
let filteredArray = list.filter { namePredicate!.evaluateWithObject($0) };
if filteredArray.count != 0
{
let value = filteredArray[0] as NSDictionary
txtVal = value.objectForKey("Name") as String
}
i have array of customer ,each customer having name,phone number and other stubs .so i used the below code to search by phone number in the array of dictionary in search bar
for index in self.customerArray
{
var string = index.valueForKey("phone")
if let phoneNumber = index.valueForKey("phone") as? String {
string = phoneNumber
}
else
{
string = ""
}
if string!.localizedCaseInsensitiveContainsString(searchText) {
filtered.addObject(index)
searchActive = true;
}
}

Resources