88#import < UIKit/UIKit.h>
99
1010#import " flutter/fml/logging.h"
11- #import " flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
11+
12+ FLUTTER_ASSERT_ARC
1213
1314// Method Channel name to start spell check.
1415static NSString * const kInitiateSpellCheck = @" SpellCheck.initiateSpellCheck" ;
1516
17+ @interface FlutterSpellCheckResult : NSObject
18+
19+ @property (nonatomic , copy , readonly ) NSArray <NSString*>* suggestions;
20+ @property (nonatomic , assign , readonly ) NSRange misspelledRange;
21+
22+ - (instancetype )init NS_UNAVAILABLE;
23+ + (instancetype )new NS_UNAVAILABLE;
24+ - (instancetype )initWithMisspelledRange : (NSRange )range
25+ suggestions : (NSArray <NSString*>*)suggestions NS_DESIGNATED_INITIALIZER;
26+ - (NSDictionary <NSString*, NSObject *>*)toDictionary ;
27+
28+ @end
29+
1630@interface FlutterSpellCheckPlugin ()
1731
18- @property (nonatomic , retain ) UITextChecker* textChecker;
32+ @property (nonatomic ) UITextChecker* textChecker;
1933
2034@end
2135
2236@implementation FlutterSpellCheckPlugin
2337
2438- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
25- if (!_textChecker ) {
39+ if (!self. textChecker ) {
2640 // UITextChecker is an expensive object to initiate, see:
2741 // https://github.com/flutter/flutter/issues/104454. Lazily initialate the UITextChecker object
2842 // until at first method channel call. We avoid using lazy getter for testing.
29- _textChecker = [[UITextChecker alloc ] init ];
43+ self. textChecker = [[UITextChecker alloc ] init ];
3044 }
3145 NSString * method = call.method ;
3246 NSArray * args = call.arguments ;
@@ -88,13 +102,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
88102 }
89103 } while (nextSpellSuggestion != nil && nextOffset < text.length );
90104
91- NSMutableArray * methodChannelResult = [[[ NSMutableArray alloc ] init ] autorelease ];
105+ NSMutableArray * methodChannelResult = [[NSMutableArray alloc ] init ];
92106
93107 for (FlutterSpellCheckResult* result in allSpellSuggestions) {
94108 [methodChannelResult addObject: [result toDictionary ]];
95109 }
96110
97- [allSpellSuggestions release ];
98111 return methodChannelResult;
99112}
100113
@@ -121,19 +134,8 @@ - (FlutterSpellCheckResult*)findSpellCheckSuggestionsForText:(NSString*)text
121134 NSArray <NSString *>* suggestions = [self .textChecker guessesForWordRange: misspelledRange
122135 inString: text
123136 language: language];
124- FlutterSpellCheckResult* result =
125- [[[FlutterSpellCheckResult alloc ] initWithMisspelledRange: misspelledRange
126- suggestions: suggestions] autorelease ];
127- return result;
128- }
129-
130- - (UITextChecker*)textChecker {
131- return _textChecker;
132- }
133-
134- - (void )dealloc {
135- [_textChecker release ];
136- [super dealloc ];
137+ return [[FlutterSpellCheckResult alloc ] initWithMisspelledRange: misspelledRange
138+ suggestions: suggestions];
137139}
138140
139141@end
@@ -151,18 +153,14 @@ - (instancetype)initWithMisspelledRange:(NSRange)range
151153}
152154
153155- (NSDictionary <NSString*, NSObject *>*)toDictionary {
154- NSMutableDictionary * result = [[[NSMutableDictionary alloc ] initWithCapacity: 3 ] autorelease ];
155- result[@" startIndex" ] = @(_misspelledRange.location );
156- // The end index represents the next index after the last character of a misspelled word to match
157- // the behavior of Dart's TextRange: https://api.flutter.dev/flutter/dart-ui/TextRange/end.html
158- result[@" endIndex" ] = @(_misspelledRange.location + _misspelledRange.length );
159- result[@" suggestions" ] = _suggestions;
160- return result;
161- }
162-
163- - (void )dealloc {
164- [_suggestions release ];
165- [super dealloc ];
156+ return @{
157+ @" startIndex" : @(_misspelledRange.location ),
158+ // The end index represents the next index after the last character of a misspelled word to
159+ // match the behavior of Dart's TextRange:
160+ // https://api.flutter.dev/flutter/dart-ui/TextRange/end.html
161+ @" endIndex" : @(_misspelledRange.location + _misspelledRange.length ),
162+ @" suggestions" : _suggestions,
163+ };
166164}
167165
168166@end
0 commit comments