我有一个NSattributedstring数组。我想获取数组中字符串的索引。我使用以下代码。
let textToSearch = "My name is Amrit"
let index = find(myArray, textToSearch)
myArray是一个NsattributedString数组。由于数组和textTosearch的类型都不同。
indexArray = ["1. THE OPENING","2. THE COW","3. THE FAMILY OF IMRAN","4. WOMEN","5. THE FEAST","6. LIVESTOCK","7. THE HEIGHTS","8. BATTLE GAINS","9. REPENTANCE","10. JONAH","11. HUD","12. JOSEPH","13. THUNDER","14. ABRAHAM","15. AL-HIJR","16. THE BEE","17. THE NIGHT JOURNEY","18. THE CAVE","19. MARY","20. TA HA","21. THE PROPHETS","22. THE PILGRIMAGE","23. THE BELIEVERS","24. LIGHT","25. THE DIFFERENTIATOR","26. THE POETS","27. THE ANTS","28. THE STORY","29. THE SPIDER","30. THE BYZANTINES","31. LUQMAN","32. BOWING DOWN IN WORSHIP","33. THE JOINT FORCES","34. SHEBA","35. THE CREATOR","36. YA SIN","37. RANGED IN ROWS","38. SAD","39. THE THRONGS","40. THE FORGIVER","41. [VERSES] MADE DISTINCT","42. CONSULTATION","43. ORNAMENTS OF GOLD","44. SMOKE","45. KNEELING","46. THE SAND DUNES","47. MUHAMMAD","48. TRIUMPH","49. THE PRIVATE ROOMS","50. QAF","51. SCATTERING [WINDS]","52. THE MOUNTAIN","53. THE STAR","54. THE MOON","55. THE LORD OF MERCY","56. THAT WHICH IS COMING","57. IRON","58. THE DISPUTE","59. THE GATHERING [OF FORCES]","60. WOMEN TESTED","61. SOLID LINES","62. THE DAY OF CONGREGATION","63. THE HYPOCRITES","64. MUTUAL NEGLECT","65. DIVORCE","66. PROHIBITION","67. CONTROL","68. THE PEN","69. THE INEVITABLE HOUR","70. THE WAYS OF ASCENT","71. NOAH","72. THE JINN","73. ENFOLDED","74. WRAPPED IN HIS CLOAK","75. THE RESURRECTION","76. MAN","77. [WINDS] SENT FORTH","78. THEANNOUNCEMENT","79. THE FORCEFUL CHARGERS","80. HE FROWNED","81. SHROUDED IN DARKNESS","82. TORN APART","83. THOSE WHO GIVE SHORT MEASURE","84. RIPPED APART","85. THE TOWERING CONSTELLATIONS","86. THE NIGHT-COMER","87. THE MOST HIGH","88. THE OVERWHELMING EVENT","89. DAYBREAK","90. THE CITY","91. THE SUN","92. THE NIGHT","93. THE MORNING BRIGHTNESS","94. RELIEF","95. THE FIG","96. THE CLINGING FORM","97. THE NIGHT OF GLORY","98. CLEAR EVIDENCE","99. THE EARTHQUAKE","100. THE CHARGING STEEDS","101. THE CRASHING BLOW","102. STRIVING FOR MORE","103. THE DECLINING DAY","104. THE BACKBITER","105. THE ELEPHANT","106. QURAYSH","107. COMMON KINDNESSES","108. ABUNDANCE","109. THE DISBELIEVERS","110. HELP","111. PALM FIBRE","112. PURITY [OF FAITH]","113. DAYBREAK","114. PEOPLE"]
if let rtfPath = NSBundle.mainBundle().URLForResource("quaran3", withExtension: "rtf") {
let attributedStringWithRtf = NSMutableAttributedString(fileURL: rtfPath, options: [NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType], documentAttributes: nil, error: nil)!
var lengthOfRtf = attributedStringWithRtf.length
divideFactor = Int(lengthOfRtf/endIndex)
println(divideFactor)
self.updateTextFont(attributedStringWithRtf) (valueFactor: divideFactor) (totalRange: lengthOfRtf)
self.getTheIndexNumber(attributedStringWithRtf) (valueFactor: divideFactor)
}
func updateTextFont(mystring:NSMutableAttributedString) (valueFactor:Int) (totalRange : Int) {
let screenSizeMain: CGRect = UIScreen.mainScreen().bounds
let myAttriText:NSMutableAttributedString = mystring.mutableCopy() as! NSMutableAttributedString
myAttriText.beginEditing()
myAttriText.enumerateAttributesInRange(NSMakeRange(0, myAttriText.length), options: NSAttributedStringEnumerationOptions.Reverse) { (attribute, range, stop) -> Void in
var mutableAttributes = NSDictionary(dictionary: attribute)
var font:UIFont = mutableAttributes.objectForKey(NSFontAttributeName) as! UIFont
var newFont:UIFont = font.fontWithSize(font.pointSize)
if DeviceType.IS_IPAD
{
newFont = font.fontWithSize(font.pointSize+7)
}
var fontProperties:UIFontDescriptor = font.fontDescriptor()
let sizeNumber:Float = fontProperties.fontAttributes()[UIFontDescriptorSizeAttribute] as! Float
myAttriText.addAttribute(NSFontAttributeName, value: newFont, range: range)
}
for var i=0; i <= valueFactor; i++ {
// if valueFactor*
if startIndex == 885819 {
}
//let checkCurrentRange = valueFactor*endIndex
let exactEndIndex : Int = endIndex
if (totalRange - startIndex) < endIndex
{
endIndex = totalRange - startIndex
let rangeFinal = NSMakeRange(startIndex, endIndex)
var nsTextFinal = myAttriText.attributedSubstringFromRange(rangeFinal)
pageText.append(nsTextFinal)
endIndex = exactEndIndex
return
}
let range = NSMakeRange(startIndex, endIndex)
var nsText = myAttriText.attributedSubstringFromRange(range)
for index in endIndex..<endIndex+100
{
let rangeNew = NSMakeRange(endIndex + startIndex-1,1)
var checkSpace = myAttriText.attributedSubstringFromRange(rangeNew)
let stirngNew = checkSpace.string
if stirngNew == " "
{
break;
}
else
{
endIndex++
}
}
let rangeFinal = NSMakeRange(startIndex, endIndex)
var nsTextFinal = myAttriText.attributedSubstringFromRange(rangeFinal)
pageText.append(nsTextFinal)
println(startIndex)
startIndex = startIndex + endIndex
endIndex = exactEndIndex
}
myAttriText.endEditing()
}
func visibleRangeOfTextView(textView: NSAttributedString) -> NSRange {
return NSMakeRange(startIndex, endIndex)
}
func getTheIndexNumber(attribSring:NSMutableAttributedString) (valueFactor:Int) //-> NSMutableArray
{
for index in 0..<indexArray.count {
let text = indexArray.objectAtIndex(index) as! NSString
var lengthOfRtf = attribSring.length
var textNew = attribSring.attributedSubstringFromRange(NSMakeRange(0, lengthOfRtf))
var stirngNew : NSMutableString = attribSring.mutableString.mutableCopy() as! NSMutableString
// println(stirngNew)
var range: NSRange = (stirngNew as NSString).rangeOfString(text as String)
var checkSpace = attribSring.attributedSubstringFromRange(range)
let stirngNewCheck = checkSpace.string
let pageIndex = (range.location / endIndex)
indeArryInt.addObject(pageIndex)
}
}
答案 0 :(得分:0)
你可以试试这个:
let index = myArray.indexOf { $0.string == textToSearch }
这会返回Int?
,这是找到匹配项的第一个索引。如果没有匹配,则会返回nil
。
答案 1 :(得分:0)
如果您使用的是Swift 1.2,则需要将NSAttributedString数组映射到String数组,以便可以使用正确的值调用find函数
let index = find(myArray.map({ attributedString in
return attributedString.string
}), textToSearch)
@ZoffDino的答案就像Swift 2.0中的魅力一样