How to compare URLs when one is in /Users/john format and other in file://localhost/Users/etc - objective-c

I am enumerating through directories which returns URLs in the form:
file://localhost/Users/john/Documents/static.gif
I want to check these results against URLs in the form of:
/Users/john
Specifically, I want to know if the first URL is contained in the second.
I've been going through the various NSURL methods and can't find a method that will allow me to convert one form into the other for easy comparison, or actually do the comparison for me.

You can use the path method to get the strings. The first URL will become #"/localhost/Users/john/Documents/static.gif" and second remains the same.
You can check where second URL contains the first using,
if ( [[URL1 path] hasPrefix:[URL2 path]] ) {
NSLog(#"Contained");
}

Related

Add path segment at last part of URL with ZnUrl

I am using Pharo 3 and I want to add a path segment as the last part of an URL for example http://example.com/myapp?key1=param1&key2=param2 and I want to get /myParam added to the last part. With ZnUrl I tried with #addSegment:
(ZnUrl fromString: 'http://example.com/myapp?key1=param1&key2=param2')
addPathSegment: 'myParam'
but results in
http://example.com/myapp/myParam?key1=param1&key2=param2
How could I configure the ZnUrl to get?
http://example.com/myapp?key1=param1&key2=param2/myParam
The thing you are describing is not a valid URL:
So what you are talking about is not an addition of a path segment, but rather string concatenation.
You can consider doing:
ZnUrl fromString: 'http://example.com/myapp?key1=param1&key2=param2/myParam'
or if you get a url from somewhere else,
(self asString, '/myParam') asUrl
should work too.
You can also do more magic to get everything to work, but in a first place you have to redesign your URL structure, to fit the standards (if you can influence it)

Is a RESTful PUT with no data "kosher," or should a DELETE always be used?

I have a RESTful route that works on an array field of a resource, such:
PUT /:id/mylist
When I do a PUT, I throw an error if the input is empty. That is, if an empty array is passed. I require at least one element in the array. So if the resource has an array of nine elements, and the route is called to PUT three, those three replace the existing nine.
But you cannot pass in no elements, because that would erase the nine and leave nothing.
Having no element IS ALLOWED, however - it just seems to me that allowing the array to be "cleared" in a PUT is wrong, and that it should only be done thusly:
DELETE /:id/mylist
Am I wrong? Are both okay? Is one preferred over the other?
I would think that doing DELETE on a list resource would infer that the list is no longer there and future GET requests to the URL would return a 404.
However, doing a PUT with an empty list would cause future GET requests to return a 200 and an empty list (however that is represented).
I would say both are valid approaches, it just depends one what are the most natural semantics for the resource.

ios - Compare values of an array at runtime, not knowing what they are going to be

I have 2 places where I require a UIPickerView in my app and the values that the second represents is dependent on the result of the first one.
However the values of the first one can be changed at run time and so I do not know what they are going to be. This leaves me out the hardcoding option of doing it this way:
if([_menuCategoryPickerFld.text isEqualToString:#"food_breakfast"])
{
}
This would require hardcoding values into my app.
Now the source of my data is MySQL imported via JSON, so when I select an option in the first UIPickerView, it will query and return the correct JSON file.
I need to know if I can compare a value of an array that I will only receive once the app is running??
Thanks in advance.
Not sure I understand, but why not just:
NSString* mystery = #"blah";
if([_menuCategoryPickerFld.text isEqualToString:mystery]){ ... }
You don't need to the the contents of a string to use it in a compare this way.

Change Url using Regex

I have url, for example:
http://i.myhost.com/myimage.jpg
I want to change this url to
http://i.myhost.com/myimageD.jpg.
(Add D after image name and before point)
i.e I want add some words after image name and before point using regex.
What is the best way do it using regex?
Try using ^(.*)\.([a-zA-Z]{3,5}) and replacing with \1D\2. I'm assuming the extension is 3-5 alphanumeric numbers but you can modify it to suit. E.g. if it's just jpg images then you can put that instead of the [a-zA-Z]{3,5}.
Sounds like a homework question given the solution must use a regex, on that assumption here is an outline to get you going.
If all you have is a URL then #mathematical.coffee's solution will suit. However if you have a chunk of text within which is one or more URLs and you have to locate and change just those then you'll need something a little more involved.
Look at the structure of a URL: {protocol}{address}{item}; where
{protocol} is "http://", "ftp://" etc.;
{address} is a name, e.g. "www.google.com", or a number, e.g. "74.125.237.116" - there will always be at least one dot in the address; and
{item} is "/name" where name is quite flexible - there will be zero or more items, you can think of them as directories and a file but this isn't strictly true. Also the sequence of items can end in a "/" (including when there are zero of them).
To make a regex which matches a URL start by matching each part. In the case of the items you'll want to match the last in the sequence separately - you'll have zero or more "directories" and one "file", the latter must be of the form "name.extension".
Once you have regexes for each part you just concatenate them to produce a regex for the whole. To form the replacement pattern you can surround parts of your regex with parentheses and refer to those parts using \number in the replacement string - see #mathematical.coffee's solution for an example.
The best way to learn regexs is to use an editor which supports them and just experiment. The exact syntax may not be the same as NSRegularExpression but they are mostly pretty similar for the basic stuff and you can translate from one to another easily.

Parse domain name from URL string

How would I parse a domain name in Objective-C?
For example if my string value was "http://www.google.com" I would like to parse out the string "google"
I think the question is a tiny bit invalid. A host is determined by its FQDN (fully qualified domain name) which, in your example, is www.google.com. It's not the same as mail.google.com or www.google.info or google.com. To single out "google" is not trivial and does not make much sense from URL perspective.
If you'd like to just parse the URL more-or-less intelligently, I think you can do the following:
Use NSURL's -host method to get the scheme and path/query stripped correctly.
Use NSString's -componentsSeparatedByString: method to get an array of the domain name's "components".
Ignore the last component.
If there's only one component left (or it may be enough to take the second-last component), you're done.
If the first component contains "www" like www3, "ftp", "mail" or something of their kind, you can ignore it too if you like. The rest may be of interest, depending on your needs.
Test your algorithm against ten thousand URLs to get a sense of futility of this task ;)
In iOS 7 you can use the NSURLComponents class:
NSURLComponents *components = [[NSURLComponents alloc] initWithString:#"http://stackoverflow.com/questions/2333972/objective-c-parse-domain-name-from-url-string"];
NSAssert([components.host isEqualToString:#"stackoverflow.com"], nil);