8
8
#import < UIKit/UIKit.h>
9
9
10
10
#import " flutter/fml/logging.h"
11
- #import " flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
11
+
12
+ FLUTTER_ASSERT_ARC
12
13
13
14
// Method Channel name to start spell check.
14
15
static NSString * const kInitiateSpellCheck = @" SpellCheck.initiateSpellCheck" ;
15
16
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
+
16
30
@interface FlutterSpellCheckPlugin ()
17
31
18
- @property (nonatomic , retain ) UITextChecker* textChecker;
32
+ @property (nonatomic ) UITextChecker* textChecker;
19
33
20
34
@end
21
35
22
36
@implementation FlutterSpellCheckPlugin
23
37
24
38
- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
25
- if (!_textChecker ) {
39
+ if (!self. textChecker ) {
26
40
// UITextChecker is an expensive object to initiate, see:
27
41
// https://github.com/flutter/flutter/issues/104454. Lazily initialate the UITextChecker object
28
42
// until at first method channel call. We avoid using lazy getter for testing.
29
- _textChecker = [[UITextChecker alloc ] init ];
43
+ self. textChecker = [[UITextChecker alloc ] init ];
30
44
}
31
45
NSString * method = call.method ;
32
46
NSArray * args = call.arguments ;
@@ -88,13 +102,13 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
88
102
}
89
103
} while (nextSpellSuggestion != nil && nextOffset < text.length );
90
104
91
- NSMutableArray * methodChannelResult = [[[NSMutableArray alloc ] init ] autorelease ];
105
+ NSMutableArray * methodChannelResult =
106
+ [[NSMutableArray alloc ] initWithCapacity: allSpellSuggestions.count];
92
107
93
108
for (FlutterSpellCheckResult* result in allSpellSuggestions) {
94
109
[methodChannelResult addObject: [result toDictionary ]];
95
110
}
96
111
97
- [allSpellSuggestions release ];
98
112
return methodChannelResult;
99
113
}
100
114
@@ -121,19 +135,8 @@ - (FlutterSpellCheckResult*)findSpellCheckSuggestionsForText:(NSString*)text
121
135
NSArray <NSString *>* suggestions = [self .textChecker guessesForWordRange: misspelledRange
122
136
inString: text
123
137
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 ];
138
+ return [[FlutterSpellCheckResult alloc ] initWithMisspelledRange: misspelledRange
139
+ suggestions: suggestions];
137
140
}
138
141
139
142
@end
@@ -151,18 +154,14 @@ - (instancetype)initWithMisspelledRange:(NSRange)range
151
154
}
152
155
153
156
- (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 ];
157
+ return @{
158
+ @" startIndex" : @(_misspelledRange.location ),
159
+ // The end index represents the next index after the last character of a misspelled word to
160
+ // match the behavior of Dart's TextRange:
161
+ // https://api.flutter.dev/flutter/dart-ui/TextRange/end.html
162
+ @" endIndex" : @(_misspelledRange.location + _misspelledRange.length ),
163
+ @" suggestions" : _suggestions,
164
+ };
166
165
}
167
166
168
167
@end
0 commit comments