0

I'm trying for hours to translate this short code. Objective C :

NSString *urlStr = [request.URL absoluteString];
NSArray *urlParts = [urlStr componentsSeparatedByString:[NSString stringWithFormat:@"%@/", kREDIRECTURI]];

if (urlParts.count > 1)
{
    urlStr = urlParts[1];
    NSRange token = [urlStr rangeOfString:@"#access_token="];

    if (token.location != NSNotFound)
    {
        vc.access_token = [urlStr substringFromIndex:NSMaxRange(token)];
    }
}

What i have tried :

Swift :

var urlParts : NSArray = urlStr!.componentsSeparatedByString("\(kREDIRECTURI)")
if urlParts.count > 1
{
    urlStr = urlParts[1] as? String
    var token  = urlStr!.rangeOfString("#access_token=", options: NSStringCompareOptions.allZeros) as NSRange!

    if token != nil
    {
         var vc = ViewController()
         urlStr!.substringFromIndex(NSMaxRange(token))!
    }
}

Any idea?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105
  • What isn't working? One small point is that you are not setting vc.access_token in the swift code. – Bijington Jun 23 '15 at 18:51
  • @Bijington I just removed that line because i know how to translate the rest lol , i just wanted to show less code. the line that i cannot resolve is : NSArray *urlParts = [urlStr componentsSeparatedByString:[NSString stringWithFormat:@"%@/", kREDIRECTURI]]; and – Roi Mulia Jun 23 '15 at 18:52
  • @Bijington vc.access_token = [urlStr substringFromIndex:NSMaxRange(token)]; – Roi Mulia Jun 23 '15 at 18:52
  • You can't cast Range to NSRange in this line. var token = urlStr!.rangeOfString("#access_token=", options: NSStringCompareOptions.allZeros) as NSRange! – Epic Byte Jun 23 '15 at 19:10
  • @EpicByte ya i know , im trying to find the right way. – Roi Mulia Jun 23 '15 at 19:11
  • @roimulia Do you need to use NSRange? Why don't you just use the Swift Range? – Epic Byte Jun 23 '15 at 19:11
  • actually I'm translating the code that is working(the objective c part), im just trying to copy it as needed so it will work – Roi Mulia Jun 23 '15 at 19:12
  • @roimulia If you cast your string to NSString then you will get an NSRange instead of a Range. – Epic Byte Jun 23 '15 at 19:13

1 Answers1

1

I will assume that the access_token query parameter is at the end and the code follows the string like this 'http:\...#access_token=', the url ends with the code at the end. Here is a simple method that would extract the code out of the url,

func getTokenFromUrl(url: String) -> String? {

    let range1 = url.rangeOfString("#access_token")

    guard let range = range1 else { return nil }

    let startIndex = range.endIndex.successor()
    let rangeOfToken = url.endIndex

    let rangeOfString = startIndex ..< rangeOfToken

   return  url.substringWithRange(rangeOfString)
}

let url1 = "http://stackoverflow.com/questions/31010975/translate-rangeofstring-from-objective-c-to-swift?#access_token=873jasf82jmsa8sd"

let url2 = "http://stackoverflow.com/questions/31010975/translate-rangeofstring-from-objective-c-to-swift"

getTokenFromUrl(url1) // returns 873jasf82jmsa8sd
getTokenFromUrl(url2) // returns nil

For earlier version of Swift, you could use if let patter to unwrap the optional,

func getTokenFromUrl(url: String) -> String? {

    let range1 = url.rangeOfString("#access_token")

    if let range = range1 {

        let startIndex = range.endIndex.successor()
        let rangeOfToken = url.endIndex

        let rangeOfString = startIndex ..< rangeOfToken

       return  url.substringWithRange(rangeOfString)
    }
    return nil
}
Sandeep
  • 20,908
  • 7
  • 66
  • 106