Swift How to remove optional String word - swift2

How do I remove Optional word
public func getCurrencySymbolFromCurrencyCode(currencyCode: String) -> String {
// let currencyCode: String = "EUR"
let locale: NSLocale = NSLocale(localeIdentifier: currencyCode)
let currencySymbol: String = "\(locale.displayNameForKey(NSLocaleCurrencySymbol, value: currencyCode))"
print("Currency Symbol : \(currencySymbol)")
return currencySymbol
}
Above code produces as follow:
Optional("$")
My question is why there is Optional("$") there, how can I remove the optional and just printing $

//replace your code with this your are not force unwrapping the symbol
public func getCurrencySymbolFromCurrencyCode(currencyCode: String) -> String! {
// let currencyCode: String = "EUR"
let locale: NSLocale = NSLocale(localeIdentifier: currencyCode)
let symbol = locale.displayNameForKey(NSLocaleCurrencySymbol, value: currencyCode)!
let currencySymbol: String = "\(symbol)"
print("Currency Symbol : \(currencySymbol)")
return currencySymbol
}

Related

Generic parameter 'Self' could not be inferred (swift 4)

I want to make a substring extension for string , I tried below 2 ways but unsuccessful:
extension String
{
func substringToFirstChar(of char: Character) -> String
{
let pos = self.range(of: String(char))
let subString = self[..<pos?.lowerBound]
return String(subString)
}
}
or
extension String
{
func substringToFirstChar(of char: Character) -> String
{
let pos = self.index(of: char)
let subString = self[..<pos]
return String(subString)
}
}
xcode prompt error: Generic parameter 'Self' could not be inferred at the "let subString = self[.." line.
How to do that ?
range(of / index(of returns an optional. You have to unwrap the optional in the range expression
extension String
{
func substringToFirstChar(of char: Character) -> String?
{
guard let pos = self.range(of: String(char))?.lowerBound else { return nil }
// or guard let pos = self.index(of: char) else { return nil }
let subString = self[..<pos]
return String(subString)
}
}
alternatively – to avoid the optional – return the unchanged string if there is no match
extension String
{
func substringToFirstChar(of char: Character) -> String
{
guard let pos = self.range(of: String(char))?.lowerBound else { return self }
// or guard let pos = self.index(of: char) else { return self }
let subString = self[..<pos]
return String(subString)
}
}

How to use thousand separator swift

func showNumbers(){
if let inputString = numberInput.text {
let input = Int(inputString)
let nums = input?.formattedWithSeparator
let group = Int(round(groupslider.value))
let priceEach = Int(round(Double((nums)!/group*100))/100)
perperson.text = String(priceEach)
}
}
}
extension Formatter {
static let withSeparator: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.numberStyle = .decimal
return formatter
}()
}
extension BinaryInteger {
var formattedWithSeparator: String {
return Formatter.withSeparator.string(for: self) ?? ""
}
}
I have two places that I want to make it like 1,000,000
input String and perperson.text
what should I use? NSNumberForatter?
I want to use thousandSeparator or groupSeparator.
I get " Binary operator '/' cannot be applied to operands of type 'String' and 'Int' " this error message.

Instance member cannot be use on type

#IBAction func saveDetails(sender: AnyObject) {
Person.firstName = firstNameTF.text
Person.lastName = lastNameTF.text
}
Above is the function I am trying to implement and below is the class I am trying to create an instance of and store the data from my text fields in... I am getting the error "Instance member "firstName" cannot be used on type Person". I was almost positive that my class was setup and initialised properly so I can't see what the problem could be?
class Person {
var firstName : String = ""
var middleName : String? = nil
var lastName : String = ""
var yearOfBirth : Int? = nil
var age : Int! {
get {
guard let _ = yearOfBirth else {
return nil
}
return currentYear - yearOfBirth!
}
set {
yearOfBirth = currentYear - newValue
}
}
init(firstName: String, lastName: String, yearOfBirth: Int? = nil, middleName: String? = nil){
self.firstName = firstName
self.lastName = lastName
self.yearOfBirth = yearOfBirth
self.middleName = middleName
}
convenience init(firstName: String, lastName: String, age: Int, middleName: String? = nil) {
self.init(firstName: firstName, lastName: lastName, yearOfBirth: nil, middleName: middleName)
self.age = age
}
}
The error message says you cannot call the properties on the class (type) Person.
Create a Person instance using the given initializer
#IBAction func saveDetails(sender: AnyObject) {
let person = Person(firstName:firstNameTF.text, lastName:lastNameTF.text)
// do something with person
}
You should create an instance of Person in order to set its properties:
either do this:
#IBAction func saveDetails(sender: AnyObject) {
let p = Person(firstName: firstNameTF.text!, lastName: lastNameTF.text!)
}
or add an init method that doesn't take arguments to your Person class
#IBAction func saveDetails(sender: AnyObject) {
let p = Person()
p.firstName = firstNameTF.text!
p.lastName = lastNameTF.text!
}
"Instance member "firstName" cannot be used on type Person" is perfect explanation.
class C {
var s: String = ""
static var s1: String = ""
}
C.s1 = "alfa"
//C.s = "alfa" // error: instance member 's' cannot be used on type 'C'
let c0 = C()
c0.s = "beta"
//c0.s1 = "beta" // error: static member 's1' cannot be used on instance of type 'C'

Generating HMAC-SHA256 from Byte-Array in Swift

I want to create a signature for the myJD REST-API. But the first snippet is not working. I talked to the support and they told me that I need the unhexed key to create the correct hash. So I created a Byte-Array of the Hex-Value and changed the code to work again (see second snippet). Both methods have the same result.
Link to API documentation: https://docs.google.com/document/d/1IGeAwg8bQyaCTeTl_WyjLyBPh4NBOayO0_MAmvP5Mu4/edit?pref=2&pli=1
First:
import Foundation
extension String{
func hmacSHA256(key: String) -> String {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = Int(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = Int(CC_SHA256_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)!
let keyLen = Int(key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), keyStr, keyLen, str!, strLen, result)
let digest = stringFromResult(result, length: digestLen)
result.dealloc(digestLen)
return digest
}
private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
let hash = NSMutableString()
for i in 0..<length {
hash.appendFormat("%02x", result[i])
}
return String(hash)
}
}
Second:
func hmacSHA256_2(key: String) -> String {
//let cKey = key.dataUsingEncoding(NSUTF8StringEncoding)!
let bKey = Array(key.utf8)
let bData = Array(self.utf8)
//let cData = self.dataUsingEncoding(NSUTF8StringEncoding)!
var cHMAC = [CUnsignedChar](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0)
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA256), bKey, Int(bKey.count), bData, Int(bData.count), &cHMAC)
let output = NSMutableString(capacity: Int(CC_SHA256_DIGEST_LENGTH))
for byte in cHMAC {
output.appendFormat("%02hhx", byte)
}
return output as String
}

Signal SIGABRT in Swift using NSuserdefaults

I'm building a swift game and a need to set up a class. My code works for all the elements in my class, but not for this.
func saveInformationMember(){
var MembersDefaultName = NSUserDefaults.standardUserDefaults()
MembersDefaultName.setValue(globalCurrentMembers, forKey: "globalCurrentMembersData")
MembersDefaultName.synchronize()
}
GlobalCurrentMembers is an array of Member which looks like that:
class Member {
var image = String ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func Init(){
image = "default.png"
name = "default"
progression = 0
round = 0
level = 0
}
To save your class this way, Member needs to conform to the NSCoding protocol.
Thx to Aaron Brager for is response. This is the response :
func saveInformationMember(){
let data = NSKeyedArchiver.archivedDataWithRootObject(globalCurrentMembers)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "member")}
func loadInformationMember(){
if let data = NSUserDefaults.standardUserDefaults().objectForKey("member") as? NSData {
globalCurrentMembers = NSKeyedUnarchiver.unarchiveObjectWithData(data) as [Member]
}
And my class:
class Member : NSObject, NSCoding {
var image = String ()
var name = String ()
var progression = Int()
var round = Int()
var level = Int()
var imageProgression = [UIButton]()
func initiation(){
image = "default.png"
name = "default"
progression = 0
round = 0
level = 0
}
required convenience init(coder decoder: NSCoder) {
self.init()
self.image = decoder.decodeObjectForKey("image") as String!
self.name = decoder.decodeObjectForKey("name") as String!
self.progression = decoder.decodeIntegerForKey("progression") as Int!
self.round = decoder.decodeIntegerForKey("round") as Int!
self.level = decoder.decodeIntegerForKey("level") as Int!
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.image, forKey: "image")
coder.encodeObject(self.name, forKey: "name")
coder.encodeInt(Int32(self.progression), forKey: "progression")
coder.encodeInt(Int32(self.round), forKey: "round")
coder.encodeInt(Int32(self.level), forKey: "level")
}}

Resources