How to replace specific strings to NSTextAttachment for show image - ios

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.

Related

Swift -sort array by substring

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
}

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?

Append an array of strings to a string in Swift

I have an array of strings for one variable, and a string as another variable. I'd like to append all of the strings in the collection to the single string.
So for example I have:
var s = String()
//have the CSV writer create all the columns needed as an array of strings
let arrayOfStrings: [String] = csvReport.map{GenerateRow($0)}
// now that we have all the strings, append each one
arrayOfStrings.map(s.stringByAppendingString({$0}))
the line above fails. I've tried every combination I can think of, but at the end of the day, I can't get it unless I just create a for loop to iterate through the entire collection, arrayOfStrings, and add it one by one. I feel like I can achieve this the same way using map or some other function.
Any help?
Thanks!
You can use joined(separator:):
let stringArray = ["Hello", "World"]
let sentence = stringArray.joined(separator: " ") // "Hello World"
You could convert your array to string using joinWithSeparator(String)
here is an example
var array = ["1", "2", "3"]
let stringRepresentation = array.joinWithSeparator("-") // "1-2-3"
source: [ How do I convert a Swift Array to a String? ]
There are at least two options here. The most semantic choice is likely joinWithSeparator on the [String] object. This concatenates every string in the array, placing the separator provided as a parameter between each string.
let result = ["a", "b", "c", "d"].joinWithSeparator("")
An alternative is to use a functional reduce and the + function operator which concatenates strings. This may be preferred if you want to do additional logic as part of the combine. Both example code produce the same result.
let result = ["a", "b", "c", "d"].reduce("", combine: +)
It's also worth noting the second options is transferrable to any type that can be added, whereas the first only works with a sequence of strings, as it is defined on a protocol extension of SequenceType where Generator.Element == String.

How to use split and map method?

I saw a question: Swift: Split a String into an array
And there's some code I don't understand:
let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map{String($0)}
fullNameArr[0] // First
fullNameArr[1] // Last
How does split() and map{} work?
You're using a syntax that won't work in Xcode7. The correct syntax should be
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
Getting that out of the way let's break down that line into two pieces:
split takes
A collection of Characters representing the String's extended
grapheme clusters
-- From Xcode docs
and a closure taking a character and returning Bool - true if the character can be considered as a separator.
if this syntax is confusing try reading that:
fullNameArr = fullName.characters.split({
character in
return character == " "
})
Now, split returns an array of SubSequence objects. You want to convert them back to string to be able to print them nicely. So one way of doing it would be creating a for loop iterating over all the results of split and converting them to string, then appending to a result array, or using map method that does the same.
If you look closely at the first line, you execute map on the array and pass a closure that does something with every element of the array and writes it back.
A simple example how that works
let exampleArray = [1, 2, 3]
print(exampleArray.map {$0 * 3})
// prints [3, 6, 9]
Hope that helps!

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