1- import CwlCatchException
21import Foundation
32import MapLibre
43import UIKit
@@ -11,81 +10,66 @@ import UIKit
1110 @objc public static func addImageToStyle(
1211 target: NSObject , field: String , expression: NSExpression
1312 ) {
14- if let exception = NSException . catchException ( in : {
13+ do {
1514 target. setValue ( expression, forKey: field)
16- } ) {
17- print ( " ⚠️ Couldn't set expression in Helpers.addImageToStyle(): \( exception ) " )
15+ } catch {
16+ print ( " Couldn't set expression in Helpers.addImageToStyle() " )
1817 }
1918 }
2019
2120 @objc public static func setExpression(
2221 target: NSObject , field: String , expression: NSExpression
2322 ) {
24- if let exception = NSException . catchException ( in : {
23+ do {
2524 // https://developer.apple.com/documentation/objectivec/nsobject/1418139-setvalue
26- target. setValue ( expression, forKey: field)
27- } ) {
28- print ( " ⚠️ Couldn't set expression in Helpers.setExpression(): \( exception ) " )
25+ try target. setValue ( expression, forKey: field)
26+ } catch {
27+ print ( " Couldn't set expression in Helpers.setExpression() " )
2928 }
3029 }
3130
3231 @objc public static func parseExpression(
3332 propertyName: String , expression: String
3433 ) -> NSExpression ? {
35- print ( " Helpers.parseExpression(): \( propertyName) : \( expression) " )
36- var result : NSExpression ? = nil
37-
38- if let exception = NSException . catchException ( in: {
34+ print ( " \( propertyName) : \( expression) " )
35+ do {
3936 // can't create an Expression using the default method if the data is a hex string
4037 if propertyName. contains ( " color " ) , expression. first == " # " {
41- guard let color = UIColor ( hexString: expression) else {
42- print ( " ⚠️ Couldn't create UIColor from hex string: \( expression) " )
43- return
44- }
45- result = NSExpression ( forConstantValue: color)
46- return
38+ let color = UIColor ( hexString: expression)
39+ return NSExpression ( forConstantValue: color)
4740 }
4841 if expression. starts ( with: " [ " ) {
4942 // can't create an Expression if the data of a literal is an array
50- do {
51- let json = try JSONSerialization . jsonObject (
52- with: expression. data ( using: . utf8) !,
53- options: . fragmentsAllowed
54- )
55-
56- // print("json: \(json)")
57- if let offset = json as? [ Any ] {
58- // Case 1: ["literal", [x, y]]
59- if let keyword = offset. first as? String ,
60- keyword == " literal " ,
61- offset. count == 2 ,
62- let vector = offset. last as? [ Any ] ,
63- vector. count == 2 ,
64- let x = vector. first as? Double ,
65- let y = vector. last as? Double
66- {
67- result = NSExpression (
68- forConstantValue: NSValue ( cgVector: CGVector ( dx: x, dy: y) )
69- )
70- return
43+ let json = try JSONSerialization . jsonObject (
44+ with: expression. data ( using: . utf8) !,
45+ options: . fragmentsAllowed
46+ )
47+ // print("json: \(json)")
48+ if let offset = json as? [ Any ] {
49+ if offset. count == 2 , offset. first as? String == " literal " {
50+ if let vector = offset. last as? [ Any ] {
51+ if vector. count == 2 {
52+ if let x = vector. first as? Double ,
53+ let y = vector. last as? Double
54+ {
55+ return NSExpression (
56+ forConstantValue: NSValue (
57+ cgVector: CGVector ( dx: x, dy: y) ) )
58+ }
59+ }
7160 }
72-
73- // Case 2: simple array literal like [x, y] or [5,5]
74- result = NSExpression ( forConstantValue: offset)
75- return
61+ } else if let first = offset. first, !( first is String ) {
62+ return NSExpression ( forConstantValue: offset)
7663 }
77- result = NSExpression ( mglJSONObject: json)
78- return
79- } catch {
80- print ( " ⚠️ JSON parsing error in Helpers.parseExpression(): \( error) " )
81- return
8264 }
65+ return NSExpression ( mglJSONObject: json)
8366 }
8467 // parse as a constant value
85- result = NSExpression ( forConstantValue: expression)
86- } ) {
87- print ( " ⚠️ Couldn't parse expression in Helpers.parseExpression(): \( exception) " )
68+ return NSExpression ( forConstantValue: expression)
69+
70+ } catch {
71+ print ( " Couldn't parse Expression: " + expression)
8872 }
89- return result
73+ return nil
9074 }
9175}
0 commit comments