如何使用Swift(macOS)获取相应货币代码的货币符号.
例:
我的代码:
var formatter = NSNumberFormatter()
formatter.currencySymbol = getSymbol(currencyCode)
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let number = NSNumber(double: (amount as NSString).doubleValue)
let amountWithSymbol = formatter.stringFromNumber(number)!
Run Code Online (Sandbox Code Playgroud)
getSymbol(_ currencyCode: String) -> String
或者,还有更好的方法?
我如何在 Swift 中使用下面的 Objective-C 代码,我试过了,但出了点问题。
目标-C:
NSUInteger index = [theArray indexOfObjectPassingTest:
^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
{
return [[dict objectForKey:@"name"] isEqual:theValue];
}
];
Run Code Online (Sandbox Code Playgroud)
斯威夫特(不起作用):
let index = theArray.indexOfObjectPassingTest { (var dict: NSDictionary, var ind: Int, var bool: Bool) -> Bool in
return dict.objectForKey("name")?.isEqual("theValue")
}
Run Code Online (Sandbox Code Playgroud) 我需要这样的东西:
但我的代码是这样做的:
func collectionView(collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> NSView {
let a = myView()
if kind == NSCollectionElementKindSectionHeader {
a.frame = NSMakeRect(0, 0, 1000, 10)
}
else if kind == NSCollectionElementKindSectionFooter {
a.frame = NSMakeRect(0, 0, 1000, 12)
}
return a
}
func numberOfSectionsInCollectionView(collectionView: NSCollectionView) -> Int {
return 3
}
override func numberOfItemsInSection(section: Int) -> Int {
if section == 0 {
return 5
}
else if section == 1 {
return 5
}
return …
Run Code Online (Sandbox Code Playgroud) 如何在OS X中显示带圆角的NSVisualEffectView?
我添加NSVisualEffectView的代码:
let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 300, 300))
visualEffectView.material = NSVisualEffectMaterial.Dark
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow
self.addSubview(visualEffectView)
Run Code Online (Sandbox Code Playgroud) 如何快速获取对应货币代码的 NSLocale?
编码:
func format(amount: String, code: String) -> NSDictionary {
var formatter = NSNumberFormatter()
let locale = getLocaleByCurrencyCode(code)
formatter.locale = locale
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
let number = NSNumber(double: (amount as NSString).doubleValue)
return ["locale":locale, "amount": formatter.stringFromNumber(number)!]
}
func getLocaleByCurrencyCode(code) -> NSLocale {
...
}
Run Code Online (Sandbox Code Playgroud)