使用OC swift 截取路径中的最后的文件名

使用 OC swift 截取路径中的最后的文件名

 

如何截取下面路径中最后的文件名 AppDelegate.swift

/Users/XXX/Desktop/Swift/swift02/code/02-自定义LOG/Weibo/Weibo/AppDelegate.swift 

 

使用 OC 如何截取

- (NSString *)substringFromWith:(NSString *)str

{

    NSInteger local = 0;

    while([str rangeOfString:@"/"].location != NSNotFound)

    {

        local = [str rangeOfString:@"/"].location + 1;

        str = [str substringFromIndex:local];

    }

    return str;

}

其实这个方法做成NSString 分类方法更好 

 

使用 swift 又如何截取

 

 

// T的含义: 外界传入什么就是什么

func NJLog<T>(message: T, file: NSString = __FILE__, method: String = __FUNCTION__, line: Int = __LINE__)

{

    #if DEBUG

        

    //print("\(file)")

    var file2: NSString = file

    var local: Int = 0

    while(file2.rangeOfString("/")).location != NSNotFound

    {

        local = (file2.rangeOfString("/")).location + 1;

        file2 = file2.substringFromIndex(local)

    }

 

    print("\(file2) \(method)[\(line)]: \(message)")

    #endif

}

posted on 2015-10-24 13:47  城923181  阅读(390)  评论(0编辑  收藏  举报

导航