diff --git a/Source/flash/extensions/NSArrayExtension.swift b/Source/flash/extensions/NSArrayExtension.swift index ded44ec..87d2392 100644 --- a/Source/flash/extensions/NSArrayExtension.swift +++ b/Source/flash/extensions/NSArrayExtension.swift @@ -7,6 +7,7 @@ // import Foundation + public extension NSArray { /** 将参数中指定的元素与数组中的元素连接,并创建新的数组。 @@ -47,6 +48,45 @@ public extension NSArray { return newArray; } + /** + 返回由原始数组中某一范围的元素构成的新数组,而不修改原始数组。 + 返回的数组包括 startIndex 元素以及从其开始到endIndex 元素(但不包括该元素)的所有元素。
+ 如果不传递任何参数,则新数组是原始数组的副本(浅表克隆)。
+ Returns a new array that consists of a range of elements from the original array,
+ without modifying the original array. The returned array includes
+ the startIndex element and all elements up to, but not including, the endIndex element.
+ If you don't pass any parameters, + the new array is a duplicate (shallow clone) of the original array. + + - parameter startIndex: 一个数字,指定片段起始点的索引。 + 如果 startIndex 是负数,则起始点从数组的结尾开始,其中 -1 指的是最后一个元素。
+ A number specifying the index of the starting point for the slice. + If startIndex is a negative number, the starting point begins at the end of the array, + where -1 is the last element. + + - parameter endIndex: 一个数字,指定片段终点的索引。如果省略此参数, + 则片段包括数组中从开头到结尾的所有元素。如果 endIndex 是负数, + 则终点从数组的结尾指定,其中 -1 指的是最后一个元素。
+ A number specifying the index of the ending point for the slice. + If you omit this parameter, the slice includes all elements from + the starting point to the end of the array. If endIndex is a negative number, + the ending point is specified from the end of the array, where -1 is the last element. + + + - returns: 一个数组,由原始数组中某一范围的元素组成。
+ An array that consists of a range of elements from the original array. + */ + public func slice(startIndex: Int = 0, _ endIndex: Int = Int.max) -> NSMutableArray { + var start = startIndex; + var end = endIndex; + if (start < 0) {start = length + start} + if (end < 0) {end = length + end} + if (start > length) {start = length} + if (end > length) {end = length} + if (end <= start) {return NSMutableArray()} ; + return NSMutableArray(array: self.subarrayWithRange(NSRange(location: start, length: end - start))) ; + } + /** 使用 strict equality (===) 运算符搜索数组中的项,并返回项的索引位置。
Searches for an item in an array by using strict equality (===)
diff --git a/Source/flash/extensions/NSMutableArrayExtension.swift b/Source/flash/extensions/NSMutableArrayExtension.swift
index 9b27db9..528fc4f 100644
--- a/Source/flash/extensions/NSMutableArrayExtension.swift
+++ b/Source/flash/extensions/NSMutableArrayExtension.swift
@@ -7,6 +7,7 @@
//
import Foundation
+
public extension NSMutableArray {
/**
diff --git a/Source/flash/extensions/StringExtension.swift b/Source/flash/extensions/StringExtension.swift
index e68847f..f028055 100644
--- a/Source/flash/extensions/StringExtension.swift
+++ b/Source/flash/extensions/StringExtension.swift
@@ -5,7 +5,7 @@
// Created by featherJ on 16/1/5.
// Copyright © 2016年 fancynode. All rights reserved.
//
-
+import Foundation;
public extension String {
/**
返回一个字符串,该字符串由参数中的 Unicode 字符代码所表示的字符组成。
@@ -63,6 +63,27 @@ public extension String {
return nil;
}
+ /**
+ 在 String 对象末尾追加补充参数(如果需要,将它们转换为字符串)并返回结果字符串。
+ 源 String 对象的原始值保持不变。
+ Appends the supplied arguments to the end of the String object, + converting them to strings if necessary, and returns the resulting string. + The original value of the source String object remains unchanged. + + - parameter args: 0 个或多个要连接的值。
+ Zero or more values to be concatenated. + + - returns: 由该字符串与指定的参数连接而成的新字符串。
+ A new string consisting of this string concatenated with the specified parameters. + */ + public func concat(args: AnyObject...) -> String { + var str: String = "" + for (var i = 0;i < args.count;i++) { + str += String(args[i]) + } + return str + } + /** 返回一个子字符串,该子字符串中的字符是通过从指定的 startIndex 开始,按照 len 指定的长度截取所得的。原始字符串保持不变。
Returns a substring consisting of the characters that start at the specified startIndex and with a length specified by len. The original string is unmodified.
@@ -75,7 +96,7 @@ public extension String {
- returns: 基于指定参数的子字符串。
A substring based on the specified parameters.
*/
- public func substr(startIndex: Int = 0, _ len: Int = 0x7fffffff) -> String {
+ public func substr(startIndex: Int = 0, _ len: Int = Int.max) -> String {
var start = startIndex;
if (start < 0) {
start = self.length + start;
@@ -109,7 +130,7 @@ public extension String {
- returns: 基于指定参数的子字符串。
A substring based on the specified parameters.
*/
- public func substring(startIndex: Int = 0, _ endIndex: Int = 0x7fffffff) -> String {
+ public func substring(startIndex: Int = 0, _ endIndex: Int = Int.max) -> String {
var start = min(startIndex, endIndex) ;
start = max(0, start) ;
start = min(self.length, start) ;
@@ -133,8 +154,10 @@ public extension String {
- returns: 指定子字符串的第一个匹配项的索引,或 -1。
The index of the first occurrence of the specified substring or -1.
*/
- public func indexOf(val: String, startIndex: Int = 0) -> Int{
- let range = self.rangeOfString(val)
+ public func indexOf(val: String, _ startIndex: Int = 0) -> Int {
+ let range = self.rangeOfString(val,
+ options: NSStringCompareOptions(rawValue: 0), range: Range(start: self.startIndex.advancedBy(startIndex), end: self.endIndex)
+ )
if let range = range {
return self.startIndex.distanceTo(range.startIndex)
} else {
@@ -142,6 +165,78 @@ public extension String {
}
}
+ /**
+ 从右向左搜索字符串,并返回在 startIndex 之前找到的最后一个 val 匹配项的索引。
+ 此索引从零开始,这意味着第一个字符位于索引 0 处,最后一个字符位于 string.length - 1 处。
+ 如果未找到 val,则该方法返回 -1。
+ Searches the string from right to left and returns the index of the last + occurrence of val found before startIndex. The index is zero-based, + meaning that the first character is at index 0, and the last is at string.length - 1. + If val is not found, the method returns -1. + + - parameter val: 要搜索的字符串。
+ The string for which to search. + + - parameter startIndex: 一个可选整数,指定开始搜索 val 的起始索引。 + 默认为允许的最大索引值。如果未指定 startIndex,则从字符串中的最后一项开始搜索。
+ An optional integer specifying the starting index from which to search for val. + The default is the maximum value allowed for an index. + If startIndex is not specified, the search starts at the last item in the string. + + - returns: 指定子字符串的最后一个匹配项的位置,或 -1(如果未找到)。
+ The position of the last occurrence of the specified substring or -1 if not found. + */ + public func lastIndexOf(val: String, _ startIndex: Int = Int.max) -> Int { + let range = self.rangeOfString(val, + options: .BackwardsSearch, + range: Range(start: self.startIndex, end: self.startIndex.advancedBy(startIndex)) + ) + if let range = range { + return self.startIndex.distanceTo(range.startIndex) + } else { + return -1 + } + } + + /** + 返回一个字符串,该字符串包括从 startIndex 字符一直到 endIndex 字符(但不包括该字符)之间的所有字符。 + 不修改原始 String 对象。如果未指定 endIndex 参数,此子字符串的结尾就是该字符串的结尾。 + 如果按 startIndex 索引到的字符与按 endIndex 索引到的字符相同或位于后者的右侧,则该方法返回一个空字符串。
+ Returns a string that includes the startIndex character and all characters up to, + but not including, the endIndex character. The original String object is not modified. + If the endIndex parameter is not specified, + then the end of the substring is the end of the string. + If the character indexed by startIndex is the same as or to the right of + the character indexed by endIndex, the method returns an empty string. + + - parameter startIndex: 片段起始点的从 0 开始的索引。如果 startIndex 是一个负数,则从右到左创建片段, + 其中 -1 是最后一个字符。
+ The zero-based index of the starting point for the slice. + If startIndex is a negative number, the slice is created from right-to-left, + where -1 is the last character. + + - parameter endIndex: 一个比片段终点的索引大 1 的整数。由 endIndex 参数索引的字符未包括在已提取的字符串中。 + 如果 endIndex 是一个负数,则终点根据从字符串的结尾向后数确定,其中 -1 表示最后一个字符。 + 默认为允许的最大索引值。如果省略此参数,则使用 String.length。
+ An integer that is one greater than the index of the ending point for the slice. + The character indexed by the endIndex parameter is not included in the extracted string. + If endIndex is a negative number, + the ending point is determined by counting back from the end of the string, + where -1 is the last character. The default is the maximum value allowed for an index. + If this parameter is omitted, String.length is used. + + - returns: 基于指定索引的子字符串。
+ A substring based on the specified indices. + */ + public func slice(startIndex: Int = 0, endIndex: Int = Int.max) -> String { + var start = startIndex; + var end = endIndex; + if (start < 0) {start = length + start} + if (end < 0) {end = length + end} + if (start > length) {start = length} + if (end > length) {end = length} + return substring(start, end) ; + } /** 一个整数,它指定在所指定的 String 对象中的字符数。
因为所有字符串索引都是从零开始的,所以任何字符串 x 的最后一个字符的索引都是 x.length - 1。
diff --git a/psd.swift.xcodeproj/project.xcworkspace/xcuserdata/featherJ.xcuserdatad/UserInterfaceState.xcuserstate b/psd.swift.xcodeproj/project.xcworkspace/xcuserdata/featherJ.xcuserdatad/UserInterfaceState.xcuserstate
index d2b642e..a35b286 100644
Binary files a/psd.swift.xcodeproj/project.xcworkspace/xcuserdata/featherJ.xcuserdatad/UserInterfaceState.xcuserstate and b/psd.swift.xcodeproj/project.xcworkspace/xcuserdata/featherJ.xcuserdatad/UserInterfaceState.xcuserstate differ