Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making NSDecimalNumber Codable

Tags:

swift

swift4

Is it possible to extend NSDecimalNumber to conform Encodable & Decodable protocols?

like image 520
Arda Keskiner Avatar asked Sep 19 '17 07:09

Arda Keskiner


People also ask

How do you make Codable?

The simplest way to make a type codable is to declare its properties using types that are already Codable . These types include standard library types like String , Int , and Double ; and Foundation types like Date , Data , and URL .

What does Codable mean in Swift?

Codable is the combined protocol of Swift's Decodable and Encodable protocols. Together they provide standard methods of decoding data for custom types and encoding data to be saved or transferred.

What is Codable and Encodable in Swift?

The Codable protocol in Swift is really a union of two protocols: Encodable and Decodable . These two protocols are used to indicate whether a certain struct, enum, or class, can be encoded into JSON data, or materialized from JSON data.


1 Answers

In swift you should use Decimal type. This type confirms to protocols Encodable & Decodable from the box.

If you have NSDecimalNumber type in your code it's easy to cast it to Decimal

let objcDecimal = NSDecimalNumber(decimal: 10)
let swiftDecimal = (objcDecimal as Decimal)
like image 148
Nick Rybalko Avatar answered Sep 17 '22 15:09

Nick Rybalko