我想将一个西里尔字符串音译成最接近拉丁语的字符串.例如"матрешка"=>"matreshka","водка"=>"伏特加".理想情况下,我想在NSString或其他已经知道字母表的所有内容并且可以进行对话的其他地方准备好使用方法.
但是如果iOS API中不存在这样的功能,那么我将完全满意像ruby的tr方法,它只是使用指定为参数的简单映射替换字符串中的字符.
"????".tr('?????', 'abvgd')
Run Code Online (Sandbox Code Playgroud) 我需要检查一个整数是否可以被另一个整数整除.
如果不是,我想把它四舍五入到最接近的数字.
例:
var numberOne = 3
var numberTwo = 5
Run Code Online (Sandbox Code Playgroud)
numberTwo
不是一个倍数,numberOne
因此我希望它numberTwo
最多可以达到6.
我该怎么做?谢谢
假设我有两个数组:
let letterArray = ["a", "b", "c", "d", "e"...]
let numberArray = [1, 2, 3, 4, 5, 6, 7...]
Run Code Online (Sandbox Code Playgroud)
我想组合两个数组,以便我得到一个输出
["a1", "b2", "c3", "d4", "e5"]
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我是快速编程的新手,我还没能成功找到代码,以找出两个日期,数年和数日之间的差异.我尝试了以下代码,但它没有用
let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Full
let s = form.stringFromTimeInterval( date2.timeIntervalSinceReferenceDate - date1.timeIntervalSinceReferenceDate)
Run Code Online (Sandbox Code Playgroud)
输入
Date1 ="12/March/2015"
Date2 ="1/June/2015"
输出:x年y个月z天
请指教
我正在寻找迭代器以循环模式无限迭代集合.因此,当达到收集的结束索引时,迭代器应该在start索引处返回元素.
以下解决方案似乎有效,但我希望它可以更好地制作.
public struct LoopIterator<T: Collection>: IteratorProtocol {
private let collection: T
private var startIndexOffset: T.IndexDistance
public init(collection: T) {
self.collection = collection
startIndexOffset = 0
}
public mutating func next() -> T.Iterator.Element? {
guard !collection.isEmpty else {
return nil
}
let index = collection.index(collection.startIndex, offsetBy: startIndexOffset)
startIndexOffset += T.IndexDistance(1)
if startIndexOffset >= collection.count {
startIndexOffset = 0
}
return collection[index]
}
}
extension Array {
func makeLoopIterator() -> LoopIterator<Array> {
return LoopIterator(collection: self)
}
}
// Testing...
// Will …
Run Code Online (Sandbox Code Playgroud) 如何在Swift中获得char
/ CChar
?我需要调用以下方法:
- (void)registerOption:(NSString *)longOption shortcut:(char)shortOption requirement:(GBValueRequirements)requirement;
Run Code Online (Sandbox Code Playgroud)
我可以传递char的ASCII形式,但这很烦人.有没有一种简单的方法来获取字符串中某个索引的char?
我有简单的课程User
.当我尝试调用valueForKey
key是可选值时,我收到错误.见下文:
class User : NSObject{
var name = "Greg"
var isActive:Bool?
}
var user = User()
user.isActive = true // initiated
var ageValue : AnyObject! = user.valueForKey("name")// OK
var isActive : AnyObject! = user.valueForKey("isActive") //ERROR
Run Code Online (Sandbox Code Playgroud)
<__ lldb_expr_862.User 0x7fa321d0d590> valueForUndefinedKey:]:此类与密钥isActive不符合密钥值编码.
如果我将启动该值,它将起作用:
var isActive:Bool = true
Run Code Online (Sandbox Code Playgroud)
如何使它与可选值一起使用?
我有两个数组
array1 = ["Fri","Sat","Sun"]
array2 = ["5","6","7"]
Run Code Online (Sandbox Code Playgroud)
现在我要创建一个newArray : ["Fri5", "Sat6", "Sun7"]
. 怎么做?提前致谢.
我正在为视图添加子视图,我希望它填充视图的高度和宽度.我对约束有困难.任何帮助表示赞赏.这就是我目前所拥有的:
self.view.addSubview(self.mainView)
var leftSideConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
var bottomConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
var widthConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0.0)
var heightConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Height, relatedBy: .Equal, toItem: self.view, attribute: .Height, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([leftSideConstraint, bottomConstraint, widthConstraint, heightConstraint])
Run Code Online (Sandbox Code Playgroud) 我正在尝试添加下划线样式UITextView
,但它没有应用.如果我使用"阴影"(取消注释阴影样式和注释下划线样式)我可以看到它,但由于某种原因没有应用下划线.我使用"Courier New"字体.
- (void) addDiagHighlighting: (NSMutableAttributedString*)attrString start:(int)start end:(int)end severity:(int)severity {
// ignore diags that are out of bounds
if (start > attrString.length || end > attrString.length)
return;
NSRange range = NSMakeRange(start, end - start);
UIColor *diagColor = [self getSeverityColor: severity];
// shadow
// NSShadow *shadow = [[NSShadow alloc] init];
// [shadow setShadowColor: diagColor];
// [shadow setShadowOffset: CGSizeMake (1.0, 1.0)];
// [shadow setShadowBlurRadius: 1.0];
// [attrString addAttribute:NSShadowAttributeName
// value:shadow
// range:range];
// underline
[attrString addAttributes:@{
NSUnderlineColorAttributeName : diagColor, // …
Run Code Online (Sandbox Code Playgroud)