Reputation: 303
so I'm learning how to use JSON by doing a Blogger Reader App. I want to show in a table the image that it's inside of the blog entry, so when I download the code I have something like this:
"content = "<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\nLorem ipsum dolor sit amet, Donec posuere nibh egestas fermentum consequat.</div>\n<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\n<div class=\"separator\" style=\"clear: both; text-align: center;\">\n<a href=\"https://1.bp.blogspot.com/-HrIjp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
And I need the address of the image, so I tried to simulate in playground by using this code:
let string = "<image is at: href=\u{5C}\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
let stringInit = string
let stringOperation = NSString(string: stringInit)
let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")
let intermediateTwo = stringArray[1]
let operationTwo = NSString(string: intermediateTwo)
let strinArrayTwo = operationTwo.components(separatedBy: ".png")
let image = strinArrayTwo[0] + ".png"
And it worked fine, but when I tried to use it in my app, it crashed because stringArray
had just one element. I changed this line:
let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")
to:
let stringArray = stringOperation.components(separatedBy: "href=")
And it works properly, I guess it worked in PlayGround because I had to enter the data the same way I'm looking for it, but I really don't know what else to try, so my question is, is there any way to make it work properly?
Thanks
Upvotes: 1
Views: 758
Reputation: 47906
I'm not sure how you detected your content
, if it's taken from JSON text directly, the head should be "content":
, not "content =
.
But anyway, it's actual content seems to be represented in a escaped format. (JSON text uses such format to represent JSON string, as well as debug output of Swift String.)
And when such escaped format representation is read into Swift String, some escaping sequence such as \"
is read as a single character "
.
So, when you replace some part of the String into unicode escaped representation, you need to replace \"
into \u{22}
, not \u{5c}\u{22}
.
Also you should always check the result of components(separatedBy:)
to avoid crashing with unexpected result.
So, your code should be something like this:
let string = "<image is at: href=\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
let stringArray = string.components(separatedBy: "href=\u{22}")
if stringArray.count > 1 {
let intermediateTwo = stringArray[1]
let strinArrayTwo = intermediateTwo.components(separatedBy: ".png")
if strinArrayTwo.count > 1 {
let image = strinArrayTwo[0] + ".png"
} else {
print("'.png' not found")
}
} else {
print("'href=' not found")
}
But you should better consider using regular expression to extract substrings with this sort of pattern.
let pattern = "href=\"(.*?\\.png)\""
let regex = try! NSRegularExpression(pattern: pattern)
if let match = regex.firstMatch(in: string, options: [], range: NSRange(0..<string.utf16.count)) {
let image = (string as NSString).substring(with: match.rangeAt(1))
print(image) //->https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png
} else {
print("'href=...png' not found")
}
Upvotes: 2