You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -47,48 +47,261 @@ open class ByteCountFormatter : Formatter {
47
47
}
48
48
49
49
publicrequiredinit?(coder:NSCoder){
50
-
NSUnimplemented()
50
+
super.init(coder: coder)
51
51
}
52
52
53
-
/* Shortcut for converting a byte count into a string without creating an NSByteCountFormatter and an NSNumber. If you need to specify options other than countStyle, create an instance of NSByteCountFormatter first.
/* Specify the units that can be used in the output. If NSByteCountFormatterUseDefault, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
62
-
*/
54
+
*/
63
55
openvarallowedUnits:Units=.useDefault
64
56
65
57
/* Specify how the count is displayed by indicating the number of bytes to be used for kilobyte. The default setting is NSByteCountFormatterFileCount, which is the system specific value for file and storage sizes.
66
-
*/
58
+
*/
67
59
openvarcountStyle:CountStyle=.file
68
60
69
61
/* Choose whether to allow more natural display of some values, such as zero, where it may be displayed as "Zero KB," ignoring all other flags or options (with the exception of NSByteCountFormatterUseBytes, which would generate "Zero bytes"). The result is appropriate for standalone output. Default value is YES. Special handling of certain values such as zero is especially important in some languages, so it's highly recommended that this property be left in its default state.
70
-
*/
62
+
*/
71
63
openvarallowsNonnumericFormatting:Bool=true
72
64
73
65
/* Choose whether to include the number or the units in the resulting formatted string. (For example, instead of 723 KB, returns "723" or "KB".) You can call the API twice to get both parts, separately. But note that putting them together yourself via string concatenation may be wrong for some locales; so use this functionality with care. Both of these values are YES by default. Setting both to NO will unsurprisingly result in an empty string.
74
-
*/
66
+
*/
75
67
openvarincludesUnit:Bool=true
76
68
openvarincludesCount:Bool=true
77
69
78
70
/* Choose whether to parenthetically (localized as appropriate) display the actual number of bytes as well, for instance "723 KB (722,842 bytes)". This will happen only if needed, that is, the first part is already not showing the exact byte count. If includesUnit or includesCount are NO, then this setting has no effect. Default value is NO.
79
-
*/
71
+
*/
80
72
openvarincludesActualByteCount:Bool=false
81
73
82
74
/* Choose the display style. The "adaptive" algorithm is platform specific and uses a different number of fraction digits based on the magnitude (in 10.8: 0 fraction digits for bytes and KB; 1 fraction digits for MB; 2 for GB and above). Otherwise the result always tries to show at least three significant digits, introducing fraction digits as necessary. Default is YES.
83
-
*/
75
+
*/
84
76
openvarisAdaptive:Bool=true
85
77
86
78
/* Choose whether to zero pad fraction digits so a consistent number of fraction digits are displayed, causing updating displays to remain more stable. For instance, if the adaptive algorithm is used, this option formats 1.19 and 1.2 GB as "1.19 GB" and "1.20 GB" respectively, while without the option the latter would be displayed as "1.2 GB". Default value is NO.
87
-
*/
79
+
*/
88
80
openvarzeroPadsFractionDigits:Bool=false
89
81
90
82
/* Specify the formatting context for the formatted string. Default is NSFormattingContextUnknown.
91
-
*/
83
+
*/
92
84
openvarformattingContext:Context=.unknown
93
-
}
85
+
86
+
/* A variable to store the actual bytes passed into the methods. This value is used if the includesActualByteCount property is set.
87
+
*/
88
+
privatevaractualBytes:String=""
89
+
90
+
/* Create an instance of NumberFormatter for use in various methods
91
+
*/
92
+
privateletnumberFormatter=NumberFormatter()
93
+
94
+
/* Shortcut for converting a byte count into a string without creating an NSByteCountFormatter and an NSNumber. If you need to specify options other than countStyle, create an instance of NSByteCountFormatter first.
/* Convenience method on string(for:):. Convert a byte count into a string without creating an NSNumber.
125
+
*/
126
+
openoverridefunc string(for obj:Any?)->String?{
127
+
guardlet value = obj as?Doubleelse{
128
+
returnnil
129
+
}
130
+
131
+
returnstring(fromByteCount:Int64(value))
132
+
}
133
+
134
+
/* If allowedUnits has been set this function will ensure the correct unit is used and conversion is done. The conversion is done by making use of the divide method.
/* This method accepts a byteCount and a byteSize value. Checks to see what range the byteCount falls into and then converts to the units determined by that range. The range to be used is decided by the byteSize parameter. The conversion is done by making use of the divide method.
160
+
*/
161
+
privatefunc convertValue(fromByteCount byteCount:Int64, for byteSize:[Unit:Double])->String{
0 commit comments