Skip to content

Commit 962ab98

Browse files
authored
[objective_c] Rename NSString.toString() to .toDartString() (#1898)
1 parent 2d3fc60 commit 962ab98

15 files changed

+60
-58
lines changed

pkgs/ffigen/test/native_objc_test/bad_override_test.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:ffi';
99
import 'dart:io';
1010

1111
import 'package:ffi/ffi.dart';
12+
import 'package:objective_c/objective_c.dart';
1213
import 'package:test/test.dart';
1314
import '../test_utils.dart';
1415
import 'bad_override_bindings.dart';
@@ -53,10 +54,10 @@ void main() {
5354
// contravariant.
5455
// https://github.com/dart-lang/native/issues/1220
5556
Polygon parentResult = BadOverrideParent.new1().contravariantReturn();
56-
expect(parentResult.name().toString(), 'Rectangle');
57+
expect(parentResult.name().toDartString(), 'Rectangle');
5758

5859
Polygon childResult = BadOverrideChild.new1().contravariantReturn();
59-
expect(childResult.name().toString(), 'Triangle');
60+
expect(childResult.name().toDartString(), 'Triangle');
6061
});
6162

6263
test('Covariant args', () {
@@ -67,11 +68,12 @@ void main() {
6768
final triangle = Triangle.new1();
6869

6970
var parent = BadOverrideParent.new1();
70-
expect(parent.covariantArg_(square).toString(), 'Polygon: Square');
71-
expect(parent.covariantArg_(triangle).toString(), 'Polygon: Triangle');
71+
expect(parent.covariantArg_(square).toDartString(), 'Polygon: Square');
72+
expect(
73+
parent.covariantArg_(triangle).toDartString(), 'Polygon: Triangle');
7274

7375
parent = BadOverrideChild.new1();
74-
expect(parent.covariantArg_(square).toString(), 'Rectangle: Square');
76+
expect(parent.covariantArg_(square).toDartString(), 'Rectangle: Square');
7577
expect(() => parent.covariantArg_(triangle), throwsA(isA<TypeError>()));
7678
});
7779
});

pkgs/ffigen/test/native_objc_test/block_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,16 @@ void main() {
287287
test('Nullable string block', () {
288288
// Regression test for https://github.com/dart-lang/native/issues/1537.
289289
final block = NullableStringBlock.fromFunction(
290-
(NSString? x) => '$x Cat'.toNSString());
290+
(NSString? x) => '${x?.toDartString()} Cat'.toNSString());
291291

292292
final result1 = block('Dog'.toNSString());
293-
expect(result1.toString(), 'Dog Cat');
293+
expect(result1?.toDartString(), 'Dog Cat');
294294

295295
final result2 = block(null);
296-
expect(result2.toString(), 'null Cat');
296+
expect(result2?.toDartString(), 'null Cat');
297297

298298
final result3 = BlockTester.callNullableStringBlock_(block);
299-
expect(result3.toString(), 'Lizard Cat');
299+
expect(result3?.toDartString(), 'Lizard Cat');
300300
});
301301

302302
test('Object listener block', () async {
@@ -345,7 +345,7 @@ void main() {
345345
test('NSString listener block', () async {
346346
final hasRun = Completer<void>();
347347
final block = NSStringListenerBlock.listener((NSString s) {
348-
expect(s.toString(), "Foo 123");
348+
expect(s.toDartString(), "Foo 123");
349349
hasRun.complete();
350350
});
351351

pkgs/ffigen/test/native_objc_test/category_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ void main() {
6666
test('Category on built-in type', () {
6767
final str = 'Hello'.toNSString();
6868

69-
expect(str.method().toString(), 'HelloWorld!');
70-
expect(InterfaceOnBuiltInType.staticMethod().method().toString(),
69+
expect(str.method().toDartString(), 'HelloWorld!');
70+
expect(InterfaceOnBuiltInType.staticMethod().method().toDartString(),
7171
'GoodbyeWorld!');
7272

7373
NSString str2 = str.instancetypeMethod();
74-
expect(str2.toString(), 'Hello');
74+
expect(str2.toDartString(), 'Hello');
7575
});
7676
});
7777
}

pkgs/ffigen/test/native_objc_test/global_native_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ void main() {
2727
});
2828

2929
test('Global string', () {
30-
expect(globalString.toString(), 'Hello World');
30+
expect(globalString.toDartString(), 'Hello World');
3131
globalString = 'Something else'.toNSString();
32-
expect(globalString.toString(), 'Something else');
32+
expect(globalString.toDartString(), 'Something else');
3333
globalString = 'Hello World'.toNSString();
3434
});
3535

pkgs/ffigen/test/native_objc_test/global_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ void main() {
2929
});
3030

3131
test('Global string', () {
32-
expect(lib.globalString.toString(), 'Hello World');
32+
expect(lib.globalString.toDartString(), 'Hello World');
3333
lib.globalString = 'Something else'.toNSString();
34-
expect(lib.globalString.toString(), 'Something else');
34+
expect(lib.globalString.toDartString(), 'Something else');
3535
lib.globalString = 'Hello World'.toNSString();
3636
});
3737

pkgs/ffigen/test/native_objc_test/nullable_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ void main() {
7575
test('Nullable typealias', () {
7676
// Regression test for https://github.com/dart-lang/native/issues/1701
7777
expect(NullableInterface.returnNullableAlias_(true), isNull);
78-
expect(NullableInterface.returnNullableAlias_(false)?.toString(), "Hi");
78+
expect(
79+
NullableInterface.returnNullableAlias_(false)?.toDartString(), "Hi");
7980
});
8081
});
8182
}

pkgs/ffigen/test/native_objc_test/protocol_test.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void main() {
4040

4141
// Required instance method.
4242
final result = consumer.callInstanceMethod_(protocolImpl);
43-
expect(result.toString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14');
43+
expect(
44+
result.toDartString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14');
4445

4546
// Optional instance method.
4647
final intResult = consumer.callOptionalMethod_(protocolImpl);
@@ -57,7 +58,7 @@ void main() {
5758
// Required instance method.
5859
final result =
5960
protocolImpl.instanceMethod_withDouble_("abc".toNSString(), 123);
60-
expect(result.toString(), 'ObjCProtocolImpl: abc: 123.00');
61+
expect(result.toDartString(), 'ObjCProtocolImpl: abc: 123.00');
6162

6263
// Optional instance method.
6364
final structPtr = calloc<SomeStruct>();
@@ -110,7 +111,7 @@ void main() {
110111

111112
final myProtocol = MyProtocol.implement(
112113
instanceMethod_withDouble_: (NSString s, double x) {
113-
return 'MyProtocol: $s: $x'.toNSString();
114+
return 'MyProtocol: ${s.toDartString()}: $x'.toNSString();
114115
},
115116
optionalMethod_: (SomeStruct s) {
116117
return s.y - s.x;
@@ -119,7 +120,7 @@ void main() {
119120

120121
// Required instance method.
121122
final result = consumer.callInstanceMethod_(myProtocol);
122-
expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14');
123+
expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14');
123124

124125
// Optional instance method.
125126
final intResult = consumer.callOptionalMethod_(myProtocol);
@@ -132,7 +133,7 @@ void main() {
132133
final protocolBuilder = ObjCProtocolBuilder();
133134
MyProtocol.addToBuilder(protocolBuilder,
134135
instanceMethod_withDouble_: (NSString s, double x) {
135-
return 'ProtocolBuilder: $s: $x'.toNSString();
136+
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
136137
});
137138
SecondaryProtocol.addToBuilder(protocolBuilder,
138139
otherMethod_b_c_d_: (int a, int b, int c, int d) {
@@ -142,7 +143,7 @@ void main() {
142143

143144
// Required instance method.
144145
final result = consumer.callInstanceMethod_(protocolImpl);
145-
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
146+
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
146147

147148
// Required instance method from secondary protocol.
148149
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
@@ -155,7 +156,7 @@ void main() {
155156
final protocolBuilder = ObjCProtocolBuilder();
156157
MyProtocol.instanceMethod_withDouble_.implement(protocolBuilder,
157158
(NSString s, double x) {
158-
return 'ProtocolBuilder: $s: $x'.toNSString();
159+
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
159160
});
160161
SecondaryProtocol.otherMethod_b_c_d_.implement(protocolBuilder,
161162
(int a, int b, int c, int d) {
@@ -165,7 +166,7 @@ void main() {
165166

166167
// Required instance method.
167168
final result = consumer.callInstanceMethod_(protocolImpl);
168-
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
169+
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
169170

170171
// Required instance method from secondary protocol.
171172
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
@@ -192,7 +193,7 @@ void main() {
192193
final listenerCompleter = Completer<int>();
193194
final myProtocol = MyProtocol.implementAsListener(
194195
instanceMethod_withDouble_: (NSString s, double x) {
195-
return 'MyProtocol: $s: $x'.toNSString();
196+
return 'MyProtocol: ${s.toDartString()}: $x'.toNSString();
196197
},
197198
optionalMethod_: (SomeStruct s) {
198199
return s.y - s.x;
@@ -204,7 +205,7 @@ void main() {
204205

205206
// Required instance method.
206207
final result = consumer.callInstanceMethod_(myProtocol);
207-
expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14');
208+
expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14');
208209

209210
// Optional instance method.
210211
final intResult = consumer.callOptionalMethod_(myProtocol);
@@ -223,7 +224,7 @@ void main() {
223224
MyProtocol.addToBuilderAsListener(
224225
protocolBuilder,
225226
instanceMethod_withDouble_: (NSString s, double x) {
226-
return 'ProtocolBuilder: $s: $x'.toNSString();
227+
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
227228
},
228229
voidMethod_: (int x) {
229230
listenerCompleter.complete(x);
@@ -237,7 +238,7 @@ void main() {
237238

238239
// Required instance method.
239240
final result = consumer.callInstanceMethod_(protocolImpl);
240-
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
241+
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
241242

242243
// Required instance method from secondary protocol.
243244
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
@@ -324,7 +325,7 @@ void main() {
324325
isRequired: true, isInstanceMethod: true)!;
325326
final block = InstanceMethodBlock.fromFunction(
326327
(Pointer<Void> p, NSString s, double x) {
327-
return 'DartProxy: $s: $x'.toNSString();
328+
return 'DartProxy: ${s.toDartString()}: $x'.toNSString();
328329
});
329330
proxyBuilder.implementMethod_withSignature_andBlock_(
330331
sel, signature, block.ref.pointer.cast());
@@ -354,7 +355,7 @@ void main() {
354355

355356
// Required instance method.
356357
final result = consumer.callInstanceMethod_(proxy);
357-
expect(result.toString(), "DartProxy: Hello from ObjC: 3.14");
358+
expect(result.toDartString(), "DartProxy: Hello from ObjC: 3.14");
358359

359360
// Optional instance method.
360361
final intResult = consumer.callOptionalMethod_(proxy);

pkgs/ffigen/test/native_objc_test/rename_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import 'dart:ffi';
99
import 'dart:io';
1010

11+
import 'package:objective_c/objective_c.dart';
1112
import 'package:test/test.dart';
1213
import '../test_utils.dart';
1314
import 'rename_bindings.dart';
@@ -36,7 +37,7 @@ void main() {
3637

3738
renamed.property = 123;
3839
expect(renamed.toString(), "Instance of 'Renamed'");
39-
expect(renamed.toString1().toString(), "123");
40+
expect(renamed.toString1().toDartString(), "123");
4041
});
4142

4243
test('Method with the same name as a type', () {

pkgs/ffigen/test/native_objc_test/string_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ void main() {
3030
test('NSString to/from Dart string [$s]', () {
3131
final ns1 = NSString(s);
3232
expect(ns1.length, s.length);
33-
expect(ns1.toString().length, s.length);
34-
expect(ns1.toString(), s);
33+
expect(ns1.toDartString().length, s.length);
34+
expect(ns1.toDartString(), s);
3535

3636
final ns2 = s.toNSString();
3737
expect(ns2.length, s.length);
38-
expect(ns2.toString().length, s.length);
39-
expect(ns2.toString(), s);
38+
expect(ns2.toDartString().length, s.length);
39+
expect(ns2.toDartString(), s);
4040
});
4141
}
4242

@@ -46,7 +46,7 @@ void main() {
4646

4747
final str3 = StringUtil.strConcat_with_(str1, str2);
4848
expect(str3.length, 11);
49-
expect(str3.toString(), "HelloWorld!");
49+
expect(str3.toDartString(), "HelloWorld!");
5050
});
5151
});
5252
}

pkgs/objective_c/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 5.0.0-wip
22

3+
- __Breaking change__: Rename the `NSString` to `String` conversion method from
4+
`toString()` to `toDartString()`.
35
- Add various ObjC categories (extension methods) to the built in classes.
46
- Make all visible API types public.
57

pkgs/objective_c/lib/src/ns_string.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:ffi/ffi.dart';
56
import 'objective_c_bindings_generated.dart';
67

78
extension StringToNSString on String {
89
NSString toNSString() => NSString(this);
910
}
11+
12+
extension NSStringToString on NSString {
13+
String toDartString() {
14+
final data =
15+
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
16+
return data!.bytes.cast<Utf16>().toDartString(length: length);
17+
}
18+
}

pkgs/objective_c/lib/src/objective_c_bindings_generated.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7541,13 +7541,6 @@ class NSString extends NSObject {
75417541
return nsstr;
75427542
}
75437543

7544-
@override
7545-
String toString() {
7546-
final data =
7547-
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
7548-
return data!.bytes.cast<pkg_ffi.Utf16>().toDartString(length: length);
7549-
}
7550-
75517544
NSString._(ffi.Pointer<objc.ObjCObject> pointer,
75527545
{bool retain = false, bool release = false})
75537546
: super.castFromPointer(pointer, retain: retain, release: release);

pkgs/objective_c/test/ns_input_stream_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ void main() {
232232
expect(
233233
error2,
234234
isA<NSError>()
235-
.having((e) => e.localizedDescription.toString(),
235+
.having((e) => e.localizedDescription.toDartString(),
236236
'localizedDescription', contains('some exception message'))
237-
.having((e) => e.domain.toString(), 'domain', 'DartError'));
237+
.having((e) => e.domain.toDartString(), 'domain', 'DartError'));
238238
});
239239

240240
group('delegate', () {

pkgs/objective_c/test/nsstring_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ void main() {
2222
test('NSString to/from Dart string [$s]', () {
2323
final ns1 = NSString(s);
2424
expect(ns1.length, s.length);
25-
expect(ns1.toString().length, s.length);
26-
expect(ns1.toString(), s);
25+
expect(ns1.toDartString().length, s.length);
26+
expect(ns1.toDartString(), s);
2727

2828
final ns2 = s.toNSString();
2929
expect(ns2.length, s.length);
30-
expect(ns2.toString().length, s.length);
31-
expect(ns2.toString(), s);
30+
expect(ns2.toDartString().length, s.length);
31+
expect(ns2.toDartString(), s);
3232
});
3333
}
3434
});

pkgs/objective_c/tool/data/extra_methods.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,4 @@ class NSString extends NSObject {
1010
pkg_ffi.calloc.free(cstr);
1111
return nsstr;
1212
}
13-
14-
@override
15-
String toString() {
16-
final data =
17-
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
18-
return data!.bytes.cast<pkg_ffi.Utf16>().toDartString(length: length);
19-
}
2013
}

0 commit comments

Comments
 (0)