码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: 学习笔记, swift扩展

时间:2014-07-21 09:17:39      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   2014   io   

//
//  YYExtension.swift
//
//  Created by yao_yu on 14-7-18.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import Foundation

extension String{
    func strip() -> String{
        return stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }
    
    func split(separator: String) -> [String]{
        return componentsSeparatedByString(separator)
    }
    
    var doubleValue:Double!{
    return (self as NSString).doubleValue
    }
    
    var length:Int{
    return countElements(self)
    }
    
    func isValidRange(range: Range<Int>) -> Bool{
        return range.startIndex >= 0 && range.endIndex <= length
    }
    
    subscript (range: Range<Int>) -> String {
        assert(isValidRange(range), "范围超出字符串长度")
            var startIndex:String.Index = advance(self.startIndex, range.startIndex)
            var endIndex:String.Index = advance(startIndex, range.endIndex - range.startIndex)
            return self.substringWithRange(Range(start: startIndex, end: endIndex))
    }
    
    subscript (index: Int) -> String{
        return self[index...index]
    }
    
    func at (indexes: Int...) -> [String] {
        return indexes.map { self[$0] }
    }
    
    func at (indexes: [Int]) -> [String] {
        return indexes.map { self[$0] }
    }
}

extension Array {
    func take(n: Int) -> SliceType{
        return self[0 ..< n]
    }
    
    func tail (n: Int) -> SliceType {
        return self[(count - n) ..< count]
    }
    
    func skip (n: Int) -> SliceType {
        return self[n ..< count]
    }
}

iOS: 学习笔记, swift扩展,布布扣,bubuko.com

iOS: 学习笔记, swift扩展

标签:style   blog   color   os   2014   io   

原文地址:http://www.cnblogs.com/yaoyu126/p/3856255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!