Does Swift allow a quick try-else construct? - ios

I want to do something like
doc = try? Doc(url) else Doc()
Is there a way to do something clean like that without much ado? I tried
doc = try? Doc(url) : Doc()
But that didn't work

Related

How to perform on the fly image transformations in iOS using Cloudinary

I am looking to perform image transformations using cloudinary on a web image url using the "fetch" type. I am looking to do something similar to the JAVA example for android here, but using Swift.
How can I achieve this in Swift?
Thanks!
There are many ways to tackle this, for example you can use something like that:
let options:NSDictionary = [
"transformation": CLTransformation(dictionaries: [["width":300,"height":300,"crop":"fill","gravity":"face","radius":"max","fetch_format":"auto",]]),
"type": "fetch"
]
let url:String = Cloudinary!.url("http://upload.wikimedia.org/wikipedia/commons/0/0c/Scarlett_Johansson_Césars_2014.jpg", options:options1 as! [AnyHashable: Any])
The result url in this case will be: http://res.cloudinary.com/demo/image/fetch/c_fill,f_auto,g_face,h_300,r_max,w_300/http://upload.wikimedia.org/wikipedia/commons/0/0c/Scarlett_Johansson_C%C3%A9sars_2014.jpg

F# Optional CSV load

I'm using the F# data provider to load csv files. For some reason, not in my control, they occasionally change the file to a gzip. (e.g. MyFile.txt could also be MyFile.text.gz)
So, I have this and it works just fine
let fl = CSV.load("MyFile.txt")
What I need to be able to do is if this errors with file not found, I need it to look for the alternate name.
let fl = CSV.load("MyFile.txt.gz")
I've tried a try...with block
try
let fl = CSV.load("MyFile.txt")
with
let fl = CSV.load("MyFile.txt.gz")
It won't let me use let keyword in this fashion. I even tried
try
let fl = CSV.load("MyFile.txt")
with
CSV.load("MyFile.txt.gz") -> fl
With C#, this would be pretty straight forward. Thanks in advance for any assistance.
You can use something like:
let fl =
try CSV.load("MyFile.txt")
with _ -> CSV.load("MyFile.txt.gz")
But I believe a better solution would be to check if the file exists first.

How to get rid of the starting slash in URI or URL?

I am using
URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = res.getPath();
String path2 = path.substring(1);
because the output of the method getPath() returns sth like this:
/C:/Users/......
and I need this
C:/Users....
I really need the below address because some external library refuses to work with the slash at the beginning or with file:/ at the beginning or anything else.
I tried pretty much all the methods in URL like toString() toExternalPath() etc. and done the same with URI and none of it returns it like I need it. (I totally don't understand, why it keeps the slash at the beginning).
It is okay to do it on my machine with just erasing the first char. But a friend tried to run it on linux and since the addresses are different there, it does not work...
What should with such problem?
Convert the URL to a URI and use that in the File constructor:
URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
File file = new File(res.toURI());
String fileName = file.getPath();
As long as UNIX paths are not supposed to contain drive letters, you may try this:
URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = res.getPath();
char a_char = text.charAt(2);
if (a_char==':') path = path.substring(1);
Convert to a URI, then use Paths.get().
URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = Paths.get(res.toURI()).toString();
You could probably just format the string once you get it.
something like this:
path2= path2[1:];
I was searching for one-line solution, so the best what i came up with was deleting it manually like this:
String url = this.getClass().getClassLoader().getResource(dictionaryPath).getPath().replaceFirst("/","");
In case if someone also needs to have it on different OS, you can make IF statement with
System.getProperty("os.name");

Removing a part of a URL with Ruby

Removing the query string from a URL in Ruby could be done like this:
url.split('?')[0]
Where url is the complete URL including the query string (e.g. url = http://www.domain.extension/folder?schnoo=schnok&foo=bar).
Is there a faster way to do this, i.e. without using split, but rather using Rails?
edit: The goal is to redirect from http://www.domain.extension/folder?schnoo=schnok&foo=bar to http://www.domain.extension/folder.
EDIT: I used:
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
parsed_url = URI.parse(url)
new_url = parsed_url.scheme+"://"+parsed_url.host+parsed_url.path
Easier to read and harder to screw up if you parse and set fragment & query to nil instead of rebuilding the URL.
parsed = URI::parse("http://www.domain.extension/folder?schnoo=schnok&foo=bar#frag")
parsed.fragment = parsed.query = nil
parsed.to_s
# => "http://www.domain.extension/folder"
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
u = URI.parse(url)
p = CGI.parse(u.query)
# p is now {"schnoo"=>["schnok"], "foo"=>["bar"]}
Take a look on the : how to get query string from passed url in ruby on rails
You can gain performance using Regex
'http://www.domain.extension/folder?schnoo=schnok&foo=bar'[/[^\?]+/]
#=> "http://www.domain.extension/folder"
Probably no need to split the url. When you visit this link, you are pass two parameters to back-end:
http://www.domain.extension/folder?schnoo=schnok&foo=bar
params[:schnoo]=schnok
params[:foo]=bar
Try to monitor your log and you will see them, then you can use them in controller directly.

URL manipulation: http://example.com/foo.jpg -> http://example.com/foo.preview.png

In rails I want to wrote some code to change this url string
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg
to
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.png
Should I use regular Expression to change it?
I'm new to Regexp, anyone can show me how to do this, and how to learn this stuff
thanks
If the extension is of fixed length, you're better off using string slicing.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
print url[0..-5] + ".preview" + url[-4..-1]
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpg
Or if your extensions are of variable length you can use rindex() to find the start of the extension.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpeg"
dot_index = url.rindex(".")-1
print url[0..dot_index] + ".preview" + url[dot_index+1..-1]
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpeg
If you must use a regex then do it like this:
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpeg"
print url.gsub(/\.(\w{2,4})$/, ".preview.\\1")
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpeg
If you're sure the file ends with .jpg, you can to
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
url.gsub(".jpg", ".preview.jpg")
Otherwise, you can get the filename, then append the extension.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
ext = File.extname(url)
url.gsub(ext, ".preview{ext}")
A string replace seems to be enough.
".jpg" -> ".preview.png"
Unfortunately I do not know ruby.
In python it'll be
new_url = url.replace(".jpg",".preview.png",1)
I think that it'll be similar in ruby. It seems to be sub() instead.
new_url = url.sub(".jpg",".preview.png")

Resources