diff --git a/pkgs/ffigen/test/native_objc_test/bad_override_test.dart b/pkgs/ffigen/test/native_objc_test/bad_override_test.dart index 9be1a9d4a8..1d8de8ed5e 100644 --- a/pkgs/ffigen/test/native_objc_test/bad_override_test.dart +++ b/pkgs/ffigen/test/native_objc_test/bad_override_test.dart @@ -9,6 +9,7 @@ import 'dart:ffi'; import 'dart:io'; import 'package:ffi/ffi.dart'; +import 'package:objective_c/objective_c.dart'; import 'package:test/test.dart'; import '../test_utils.dart'; import 'bad_override_bindings.dart'; @@ -53,10 +54,10 @@ void main() { // contravariant. // https://github.com/dart-lang/native/issues/1220 Polygon parentResult = BadOverrideParent.new1().contravariantReturn(); - expect(parentResult.name().toString(), 'Rectangle'); + expect(parentResult.name().toDartString(), 'Rectangle'); Polygon childResult = BadOverrideChild.new1().contravariantReturn(); - expect(childResult.name().toString(), 'Triangle'); + expect(childResult.name().toDartString(), 'Triangle'); }); test('Covariant args', () { @@ -67,11 +68,12 @@ void main() { final triangle = Triangle.new1(); var parent = BadOverrideParent.new1(); - expect(parent.covariantArg_(square).toString(), 'Polygon: Square'); - expect(parent.covariantArg_(triangle).toString(), 'Polygon: Triangle'); + expect(parent.covariantArg_(square).toDartString(), 'Polygon: Square'); + expect( + parent.covariantArg_(triangle).toDartString(), 'Polygon: Triangle'); parent = BadOverrideChild.new1(); - expect(parent.covariantArg_(square).toString(), 'Rectangle: Square'); + expect(parent.covariantArg_(square).toDartString(), 'Rectangle: Square'); expect(() => parent.covariantArg_(triangle), throwsA(isA())); }); }); diff --git a/pkgs/ffigen/test/native_objc_test/block_test.dart b/pkgs/ffigen/test/native_objc_test/block_test.dart index 5da273c5d4..8a4b7136ed 100644 --- a/pkgs/ffigen/test/native_objc_test/block_test.dart +++ b/pkgs/ffigen/test/native_objc_test/block_test.dart @@ -287,16 +287,16 @@ void main() { test('Nullable string block', () { // Regression test for https://github.com/dart-lang/native/issues/1537. final block = NullableStringBlock.fromFunction( - (NSString? x) => '$x Cat'.toNSString()); + (NSString? x) => '${x?.toDartString()} Cat'.toNSString()); final result1 = block('Dog'.toNSString()); - expect(result1.toString(), 'Dog Cat'); + expect(result1?.toDartString(), 'Dog Cat'); final result2 = block(null); - expect(result2.toString(), 'null Cat'); + expect(result2?.toDartString(), 'null Cat'); final result3 = BlockTester.callNullableStringBlock_(block); - expect(result3.toString(), 'Lizard Cat'); + expect(result3?.toDartString(), 'Lizard Cat'); }); test('Object listener block', () async { @@ -345,7 +345,7 @@ void main() { test('NSString listener block', () async { final hasRun = Completer(); final block = NSStringListenerBlock.listener((NSString s) { - expect(s.toString(), "Foo 123"); + expect(s.toDartString(), "Foo 123"); hasRun.complete(); }); diff --git a/pkgs/ffigen/test/native_objc_test/category_test.dart b/pkgs/ffigen/test/native_objc_test/category_test.dart index dbde5a95bb..ac0a38374c 100644 --- a/pkgs/ffigen/test/native_objc_test/category_test.dart +++ b/pkgs/ffigen/test/native_objc_test/category_test.dart @@ -66,12 +66,12 @@ void main() { test('Category on built-in type', () { final str = 'Hello'.toNSString(); - expect(str.method().toString(), 'HelloWorld!'); - expect(InterfaceOnBuiltInType.staticMethod().method().toString(), + expect(str.method().toDartString(), 'HelloWorld!'); + expect(InterfaceOnBuiltInType.staticMethod().method().toDartString(), 'GoodbyeWorld!'); NSString str2 = str.instancetypeMethod(); - expect(str2.toString(), 'Hello'); + expect(str2.toDartString(), 'Hello'); }); }); } diff --git a/pkgs/ffigen/test/native_objc_test/global_native_test.dart b/pkgs/ffigen/test/native_objc_test/global_native_test.dart index bc57870131..56fe7da16f 100644 --- a/pkgs/ffigen/test/native_objc_test/global_native_test.dart +++ b/pkgs/ffigen/test/native_objc_test/global_native_test.dart @@ -27,9 +27,9 @@ void main() { }); test('Global string', () { - expect(globalString.toString(), 'Hello World'); + expect(globalString.toDartString(), 'Hello World'); globalString = 'Something else'.toNSString(); - expect(globalString.toString(), 'Something else'); + expect(globalString.toDartString(), 'Something else'); globalString = 'Hello World'.toNSString(); }); diff --git a/pkgs/ffigen/test/native_objc_test/global_test.dart b/pkgs/ffigen/test/native_objc_test/global_test.dart index 113b9691b7..526afa33a5 100644 --- a/pkgs/ffigen/test/native_objc_test/global_test.dart +++ b/pkgs/ffigen/test/native_objc_test/global_test.dart @@ -29,9 +29,9 @@ void main() { }); test('Global string', () { - expect(lib.globalString.toString(), 'Hello World'); + expect(lib.globalString.toDartString(), 'Hello World'); lib.globalString = 'Something else'.toNSString(); - expect(lib.globalString.toString(), 'Something else'); + expect(lib.globalString.toDartString(), 'Something else'); lib.globalString = 'Hello World'.toNSString(); }); diff --git a/pkgs/ffigen/test/native_objc_test/nullable_test.dart b/pkgs/ffigen/test/native_objc_test/nullable_test.dart index 46ed32626e..a3721aa14e 100644 --- a/pkgs/ffigen/test/native_objc_test/nullable_test.dart +++ b/pkgs/ffigen/test/native_objc_test/nullable_test.dart @@ -75,7 +75,8 @@ void main() { test('Nullable typealias', () { // Regression test for https://github.com/dart-lang/native/issues/1701 expect(NullableInterface.returnNullableAlias_(true), isNull); - expect(NullableInterface.returnNullableAlias_(false)?.toString(), "Hi"); + expect( + NullableInterface.returnNullableAlias_(false)?.toDartString(), "Hi"); }); }); } diff --git a/pkgs/ffigen/test/native_objc_test/protocol_test.dart b/pkgs/ffigen/test/native_objc_test/protocol_test.dart index cdc9bef334..0798c7a422 100644 --- a/pkgs/ffigen/test/native_objc_test/protocol_test.dart +++ b/pkgs/ffigen/test/native_objc_test/protocol_test.dart @@ -40,7 +40,8 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(protocolImpl); - expect(result.toString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14'); + expect( + result.toDartString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14'); // Optional instance method. final intResult = consumer.callOptionalMethod_(protocolImpl); @@ -57,7 +58,7 @@ void main() { // Required instance method. final result = protocolImpl.instanceMethod_withDouble_("abc".toNSString(), 123); - expect(result.toString(), 'ObjCProtocolImpl: abc: 123.00'); + expect(result.toDartString(), 'ObjCProtocolImpl: abc: 123.00'); // Optional instance method. final structPtr = calloc(); @@ -110,7 +111,7 @@ void main() { final myProtocol = MyProtocol.implement( instanceMethod_withDouble_: (NSString s, double x) { - return 'MyProtocol: $s: $x'.toNSString(); + return 'MyProtocol: ${s.toDartString()}: $x'.toNSString(); }, optionalMethod_: (SomeStruct s) { return s.y - s.x; @@ -119,7 +120,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(myProtocol); - expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14'); + expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14'); // Optional instance method. final intResult = consumer.callOptionalMethod_(myProtocol); @@ -132,7 +133,7 @@ void main() { final protocolBuilder = ObjCProtocolBuilder(); MyProtocol.addToBuilder(protocolBuilder, instanceMethod_withDouble_: (NSString s, double x) { - return 'ProtocolBuilder: $s: $x'.toNSString(); + return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString(); }); SecondaryProtocol.addToBuilder(protocolBuilder, otherMethod_b_c_d_: (int a, int b, int c, int d) { @@ -142,7 +143,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(protocolImpl); - expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); + expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); // Required instance method from secondary protocol. final otherIntResult = consumer.callOtherMethod_(protocolImpl); @@ -155,7 +156,7 @@ void main() { final protocolBuilder = ObjCProtocolBuilder(); MyProtocol.instanceMethod_withDouble_.implement(protocolBuilder, (NSString s, double x) { - return 'ProtocolBuilder: $s: $x'.toNSString(); + return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString(); }); SecondaryProtocol.otherMethod_b_c_d_.implement(protocolBuilder, (int a, int b, int c, int d) { @@ -165,7 +166,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(protocolImpl); - expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); + expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); // Required instance method from secondary protocol. final otherIntResult = consumer.callOtherMethod_(protocolImpl); @@ -192,7 +193,7 @@ void main() { final listenerCompleter = Completer(); final myProtocol = MyProtocol.implementAsListener( instanceMethod_withDouble_: (NSString s, double x) { - return 'MyProtocol: $s: $x'.toNSString(); + return 'MyProtocol: ${s.toDartString()}: $x'.toNSString(); }, optionalMethod_: (SomeStruct s) { return s.y - s.x; @@ -204,7 +205,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(myProtocol); - expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14'); + expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14'); // Optional instance method. final intResult = consumer.callOptionalMethod_(myProtocol); @@ -223,7 +224,7 @@ void main() { MyProtocol.addToBuilderAsListener( protocolBuilder, instanceMethod_withDouble_: (NSString s, double x) { - return 'ProtocolBuilder: $s: $x'.toNSString(); + return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString(); }, voidMethod_: (int x) { listenerCompleter.complete(x); @@ -237,7 +238,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(protocolImpl); - expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); + expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14'); // Required instance method from secondary protocol. final otherIntResult = consumer.callOtherMethod_(protocolImpl); @@ -324,7 +325,7 @@ void main() { isRequired: true, isInstanceMethod: true)!; final block = InstanceMethodBlock.fromFunction( (Pointer p, NSString s, double x) { - return 'DartProxy: $s: $x'.toNSString(); + return 'DartProxy: ${s.toDartString()}: $x'.toNSString(); }); proxyBuilder.implementMethod_withSignature_andBlock_( sel, signature, block.ref.pointer.cast()); @@ -354,7 +355,7 @@ void main() { // Required instance method. final result = consumer.callInstanceMethod_(proxy); - expect(result.toString(), "DartProxy: Hello from ObjC: 3.14"); + expect(result.toDartString(), "DartProxy: Hello from ObjC: 3.14"); // Optional instance method. final intResult = consumer.callOptionalMethod_(proxy); diff --git a/pkgs/ffigen/test/native_objc_test/rename_test.dart b/pkgs/ffigen/test/native_objc_test/rename_test.dart index 9f541957fe..6f4ad1e5c1 100644 --- a/pkgs/ffigen/test/native_objc_test/rename_test.dart +++ b/pkgs/ffigen/test/native_objc_test/rename_test.dart @@ -8,6 +8,7 @@ import 'dart:ffi'; import 'dart:io'; +import 'package:objective_c/objective_c.dart'; import 'package:test/test.dart'; import '../test_utils.dart'; import 'rename_bindings.dart'; @@ -36,7 +37,7 @@ void main() { renamed.property = 123; expect(renamed.toString(), "Instance of 'Renamed'"); - expect(renamed.toString1().toString(), "123"); + expect(renamed.toString1().toDartString(), "123"); }); test('Method with the same name as a type', () { diff --git a/pkgs/ffigen/test/native_objc_test/string_test.dart b/pkgs/ffigen/test/native_objc_test/string_test.dart index 5107b24c76..f1e8a788af 100644 --- a/pkgs/ffigen/test/native_objc_test/string_test.dart +++ b/pkgs/ffigen/test/native_objc_test/string_test.dart @@ -30,13 +30,13 @@ void main() { test('NSString to/from Dart string [$s]', () { final ns1 = NSString(s); expect(ns1.length, s.length); - expect(ns1.toString().length, s.length); - expect(ns1.toString(), s); + expect(ns1.toDartString().length, s.length); + expect(ns1.toDartString(), s); final ns2 = s.toNSString(); expect(ns2.length, s.length); - expect(ns2.toString().length, s.length); - expect(ns2.toString(), s); + expect(ns2.toDartString().length, s.length); + expect(ns2.toDartString(), s); }); } @@ -46,7 +46,7 @@ void main() { final str3 = StringUtil.strConcat_with_(str1, str2); expect(str3.length, 11); - expect(str3.toString(), "HelloWorld!"); + expect(str3.toDartString(), "HelloWorld!"); }); }); } diff --git a/pkgs/objective_c/CHANGELOG.md b/pkgs/objective_c/CHANGELOG.md index 020f9f61ad..f73fc2bd87 100644 --- a/pkgs/objective_c/CHANGELOG.md +++ b/pkgs/objective_c/CHANGELOG.md @@ -1,5 +1,7 @@ ## 5.0.0-wip +- __Breaking change__: Rename the `NSString` to `String` conversion method from + `toString()` to `toDartString()`. - Add various ObjC categories (extension methods) to the built in classes. - Make all visible API types public. diff --git a/pkgs/objective_c/lib/src/ns_string.dart b/pkgs/objective_c/lib/src/ns_string.dart index 7211803161..5062e32fac 100644 --- a/pkgs/objective_c/lib/src/ns_string.dart +++ b/pkgs/objective_c/lib/src/ns_string.dart @@ -2,8 +2,17 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:ffi/ffi.dart'; import 'objective_c_bindings_generated.dart'; extension StringToNSString on String { NSString toNSString() => NSString(this); } + +extension NSStringToString on NSString { + String toDartString() { + final data = + dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */); + return data!.bytes.cast().toDartString(length: length); + } +} diff --git a/pkgs/objective_c/lib/src/objective_c_bindings_generated.dart b/pkgs/objective_c/lib/src/objective_c_bindings_generated.dart index 02d3747e43..daf5e66888 100644 --- a/pkgs/objective_c/lib/src/objective_c_bindings_generated.dart +++ b/pkgs/objective_c/lib/src/objective_c_bindings_generated.dart @@ -7541,13 +7541,6 @@ class NSString extends NSObject { return nsstr; } - @override - String toString() { - final data = - dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */); - return data!.bytes.cast().toDartString(length: length); - } - NSString._(ffi.Pointer pointer, {bool retain = false, bool release = false}) : super.castFromPointer(pointer, retain: retain, release: release); diff --git a/pkgs/objective_c/test/ns_input_stream_test.dart b/pkgs/objective_c/test/ns_input_stream_test.dart index e197a5c8c0..40682a2aee 100644 --- a/pkgs/objective_c/test/ns_input_stream_test.dart +++ b/pkgs/objective_c/test/ns_input_stream_test.dart @@ -232,9 +232,9 @@ void main() { expect( error2, isA() - .having((e) => e.localizedDescription.toString(), + .having((e) => e.localizedDescription.toDartString(), 'localizedDescription', contains('some exception message')) - .having((e) => e.domain.toString(), 'domain', 'DartError')); + .having((e) => e.domain.toDartString(), 'domain', 'DartError')); }); group('delegate', () { diff --git a/pkgs/objective_c/test/nsstring_test.dart b/pkgs/objective_c/test/nsstring_test.dart index a6e6606a4a..cf5131f60b 100644 --- a/pkgs/objective_c/test/nsstring_test.dart +++ b/pkgs/objective_c/test/nsstring_test.dart @@ -22,13 +22,13 @@ void main() { test('NSString to/from Dart string [$s]', () { final ns1 = NSString(s); expect(ns1.length, s.length); - expect(ns1.toString().length, s.length); - expect(ns1.toString(), s); + expect(ns1.toDartString().length, s.length); + expect(ns1.toDartString(), s); final ns2 = s.toNSString(); expect(ns2.length, s.length); - expect(ns2.toString().length, s.length); - expect(ns2.toString(), s); + expect(ns2.toDartString().length, s.length); + expect(ns2.toDartString(), s); }); } }); diff --git a/pkgs/objective_c/tool/data/extra_methods.dart b/pkgs/objective_c/tool/data/extra_methods.dart index f40c99a9f5..42dedc35bc 100644 --- a/pkgs/objective_c/tool/data/extra_methods.dart +++ b/pkgs/objective_c/tool/data/extra_methods.dart @@ -10,11 +10,4 @@ class NSString extends NSObject { pkg_ffi.calloc.free(cstr); return nsstr; } - - @override - String toString() { - final data = - dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */); - return data!.bytes.cast().toDartString(length: length); - } }