Skip to content

Commit 1437e1a

Browse files
authored
fix: ios literal array expressions (#398)
*sigh*
1 parent 3cc04b7 commit 1437e1a

File tree

5 files changed

+71
-69
lines changed

5 files changed

+71
-69
lines changed

maplibre_ios/ios/maplibre_ios.podspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Helper package for maplibre that provides iOS FFI bindings
1919
# Needs to be the same version as in maplibre_ios/Package.swift
2020
# FOR PREBUILT LIBRARY
2121
s.dependency 'MapLibre', '~> 6.19'
22-
s.dependency 'CwlCatchException', '~> 2.2'
2322

2423
# FOR LOCAL LIBRARY
2524
# s.vendored_frameworks = '.build/MapLibre.xcframework'

maplibre_ios/ios/maplibre_ios/Package.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ let package = Package(
1515
// Needs to be the same version as in ../maplibre_ios.podspec
1616
// FOR PREBUILT LIBRARY
1717
.package(url: "https://github.com/maplibre/maplibre-gl-native-distribution", .upToNextMinor(from: "6.19.0")),
18-
.package(url: "https://github.com/mattgallagher/CwlCatchException.git", .upToNextMinor(from: "2.2.0")),
1918
],
2019
targets: [
2120
.target(
@@ -26,8 +25,6 @@ let package = Package(
2625

2726
// FOR LOCAL LIBRARY
2827
// "MapLibre",
29-
30-
.product(name: "CwlCatchException", package: "CwlCatchException"),
3128
],
3229
cSettings: [
3330
.headerSearchPath("include/maplibre_ios"),
Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import CwlCatchException
21
import Foundation
32
import MapLibre
43
import 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
}
Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
# Use for -target the same ios version that is returned by xcrun
4-
# Build the MapLibre iOS SDK before running this script
4+
# --- Resolve Flutter SDK root dynamically ---
5+
if ! command -v flutter >/dev/null 2>&1; then
6+
echo "❌ Flutter not found in PATH. Please ensure 'flutter' is installed and available."
7+
exit 1
8+
fi
59

6-
xcrun --sdk iphonesimulator --show-sdk-path
10+
FLUTTER_ROOT=$(dirname $(dirname $(which flutter)))
11+
SDK_PATH=$(xcrun --sdk iphonesimulator --show-sdk-path)
712

13+
# --- Locate Flutter framework for the iOS simulator ---
14+
FLUTTER_FRAMEWORK_PATH="$FLUTTER_ROOT/bin/cache/artifacts/engine/ios/Flutter.xcframework/ios-arm64_x86_64-simulator"
15+
16+
if [ ! -d "$FLUTTER_FRAMEWORK_PATH" ]; then
17+
echo "❌ Flutter.xcframework not found at: $FLUTTER_FRAMEWORK_PATH"
18+
echo "Run 'flutter precache --ios' to download iOS engine artifacts."
19+
exit 1
20+
fi
21+
22+
echo "✅ Using Flutter framework at: $FLUTTER_FRAMEWORK_PATH"
23+
echo "✅ Using iOS SDK at: $SDK_PATH"
24+
25+
# --- Compile Swift sources and emit Objective-C headers ---
826
swiftc \
9-
-c MapLibreRegistry.swift \
10-
-c Extensions.swift \
11-
-c Helpers.swift \
12-
-module-name maplibre_ios \
13-
-emit-objc-header-path MapLibreIos.h \
14-
-emit-library -o libmaplibreios.dylib \
15-
-target arm64-apple-ios18.5-simulator \
16-
-sdk $(xcrun --sdk iphonesimulator --show-sdk-path) \
17-
-F ../../../.build/MapLibre.xcframework/ios-arm64_x86_64-simulator/ \
18-
-framework MapLibre
27+
-c MapLibreRegistry.swift \
28+
-c Extensions.swift \
29+
-c Helpers.swift \
30+
-module-name maplibre_ios \
31+
-emit-objc-header-path MapLibreIos.h \
32+
-emit-library -o libmaplibreios.dylib \
33+
-target arm64-apple-ios18.5-simulator \
34+
-sdk "$SDK_PATH" \
35+
-F ../../../.build/MapLibre.xcframework/ios-arm64_x86_64-simulator/ \
36+
-F "$FLUTTER_FRAMEWORK_PATH" \
37+
-framework MapLibre \
38+
-framework Flutter
39+
40+
echo "✅ Swift headers generated successfully: MapLibreIos.h"
Binary file not shown.

0 commit comments

Comments
 (0)