Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Converted to ARC #1

Closed
wants to merge 1 commit into from

1 participant

@JaredCrawford

Primarily I converted the code to ARC.

And a few other miscellaneous changes:
• Simplified some of the nested logic.
• Made singleton thread safe using GCD.

@JaredCrawford

There are deeper issues here, I recommend deleting this repository as it no longer tracks the translate API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 11, 2012
  1. Converted project to ARC, simplified some logic, and made singleton t…

    Jared Crawford authored
    …hread safe.
This page is out of date. Refresh to see the latest.
Showing with 83 additions and 89 deletions.
  1. +3 −10 MSBingTranslator.h
  2. +80 −79 MSBingTranslator.m
View
13 MSBingTranslator.h
@@ -36,16 +36,9 @@ email : moeseth@me.com
@protocol MSBingTranslatorDelegate;
@interface MSBingTranslator : NSObject
-{
-@public
- id<MSBingTranslatorDelegate> delegate;
-
-@private
- NSMutableData *receivedData;
- NSURLConnection *translate_connection, *detect_connection;
-}
-
-@property(nonatomic, retain) id<MSBingTranslatorDelegate> delegate;
+
+@property(nonatomic, weak) id<MSBingTranslatorDelegate> delegate;
+
+ (MSBingTranslator *) sharedTranslator;
- (void) initWithDelegate:(id)del andTranslateText:(NSString *)text fromLan:(NSString *)txtLan toLan:(NSString *)localLan;
- (void) initWithDelegate:(id)del andDetectLanguageTypeForText:(NSString *)text;
View
159 MSBingTranslator.m
@@ -1,45 +1,56 @@
/*
--- MSBingTranslator.m
-Copyright (c) 2011, moeseth All rights reserved.
+ -- MSBingTranslator.m
+ Copyright (c) 2011, moeseth All rights reserved.
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
-- Redistributions of source code must retain the above copyright notice, this
-list of conditions and the following disclaimer.
+ - Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
-- Redistributions in binary form must reproduce the above copyright notice,
-this list of conditions and the following disclaimer in the documentation
-and/or other materials provided with the distribution.
+ - Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-email : moeseth@me.com
-*/
+ email : moeseth@me.com
+ */
#import "MSBingTranslator.h"
-static MSBingTranslator *translator = nil;
+@interface MSBingTranslator()
+@property (nonatomic, strong) NSMutableData *receivedData;
+@property (nonatomic, strong) NSURLConnection *translate_connection;
+@property (nonatomic, strong) NSURLConnection *detect_connection;
+@end
+
@implementation MSBingTranslator
-@synthesize delegate;
+
+//public
+@synthesize delegate = _delegate;
+
+//private
+@synthesize receivedData = _receivedData;
+@synthesize translate_connection = _translate_connection;
+@synthesize detect_connection = _detect_connection;
+ (MSBingTranslator *) sharedTranslator
{
- if (translator == nil)
- {
- translator = [[self alloc] init];
- }
- return translator;
+ static dispatch_once_t once;
+ static MSBingTranslator *singleton;
+ dispatch_once(&once, ^ { singleton = [[self alloc] init]; });
+ return singleton;
}
- (void) initWithDelegate:(id)del andTranslateText:(NSString *)text fromLan:(NSString *)txtLan toLan:(NSString *)localLan
@@ -47,105 +58,95 @@ - (void) initWithDelegate:(id)del andTranslateText:(NSString *)text fromLan:(NSS
if (text.length < 1) return;
self.delegate = del;
- receivedData = [[NSMutableData alloc] init];
-
+ self.receivedData = [[NSMutableData alloc] init];
+
NSString *encodedString = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string_prefix = bingAPI_translate bingApp_ID @"&text=";
NSString *string_suffix = @"";
- if (txtLan.length > 1)
+ if (txtLan.length > 1){
string_suffix = [NSString stringWithFormat:@"&from=%@&to=%@",txtLan, localLan];
- else
+ }
+ else{
string_suffix = [NSString stringWithFormat:@"&to=%@", localLan];
-
+ }
+
NSString *finalString = [NSString stringWithFormat:@"%@%@%@", string_prefix, encodedString, string_suffix];
NSURL *queryURL = [NSURL URLWithString:finalString];
NSURLRequest *request = [NSURLRequest requestWithURL:queryURL];
- translate_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
+ self.translate_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void) initWithDelegate:(id)del andDetectLanguageTypeForText:(NSString *)text
{
if (text.length < 1) return;
-
+
self.delegate = del;
-
- receivedData = [[NSMutableData alloc] init];
-
+
+ self.receivedData = [[NSMutableData alloc] init];
+
NSString *encodedString = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *string_prefix = bingAPI_detect bingApp_ID @"&text=";
NSString *finalString = [string_prefix stringByAppendingString:encodedString];
NSURL *queryURL = [NSURL URLWithString:finalString];
NSURLRequest *request = [NSURLRequest requestWithURL:queryURL];
- detect_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
+ self.detect_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#pragma mark -
- (void) connection: (NSURLConnection*) connection didReceiveData: (NSData*) data
{
- [receivedData appendData:data];
+ [self.receivedData appendData:data];
}
- (void) connection:(NSURLConnection*)connection didFailWithError:(NSError*) error
{
NSLog(@"Translator Fail : %@", [error localizedDescription]);
-
- if ([delegate respondsToSelector:@selector(MSBingTranslator:failedWithError:)])
- [delegate MSBingTranslator:self translatedText:[error localizedDescription]];
- if (connection == translate_connection)
- {
- [translate_connection release];
- translate_connection = nil;
- } else {
- [detect_connection release];
- detect_connection = nil;
+ if ([self.delegate respondsToSelector:@selector(MSBingTranslator:failedWithError:)])
+ [self.delegate MSBingTranslator:self translatedText:[error localizedDescription]];
+
+ if (connection == self.translate_connection){
+ self.translate_connection = nil;
+ }
+ else {
+ self.detect_connection = nil;
}
- [receivedData release];
- receivedData = nil;
+ self.receivedData = nil;
}
- (void) connectionDidFinishLoading:(NSURLConnection*)connection
{
- NSString *received_txt = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
+ NSString *received_txt = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
- if (received_txt != nil)
- {
+ if (received_txt != nil){
NSArray *parts = [received_txt componentsSeparatedByString:@"/Serialization/\">"];
- if (parts.count < 1)
- {
- if ([delegate respondsToSelector:@selector(MSBingTranslator:failedWithError:)])
- [delegate MSBingTranslator:self failedWithError:@"not a valid language"];
- } else {
+ if ((parts.count < 1) && ([self.delegate respondsToSelector:@selector(MSBingTranslator:failedWithError:)])){
+ [self.delegate MSBingTranslator:self failedWithError:@"not a valid language"];
+ }
+ else {
NSString *toReturn = [parts objectAtIndex:1];
toReturn = [toReturn stringByReplacingOccurrencesOfString:@"</string>" withString:@""];
-
- if (connection == translate_connection)
- {
- if ([delegate respondsToSelector:@selector(MSBingTranslator:translatedText:)])
- [delegate MSBingTranslator:self translatedText:toReturn];
- } else
- {
- if ([delegate respondsToSelector:@selector(MSBingTranslator:detectedLanguage:)])
- [delegate MSBingTranslator:self detectedLanguage:toReturn];
+
+ if ((connection == self.translate_connection) && ([self.delegate respondsToSelector:@selector(MSBingTranslator:translatedText:)])){
+ [self.delegate MSBingTranslator:self translatedText:toReturn];
+ }
+ else if ([self.delegate respondsToSelector:@selector(MSBingTranslator:detectedLanguage:)]){
+ [self.delegate MSBingTranslator:self detectedLanguage:toReturn];
}
}
}
- [receivedData release];
- receivedData = nil;
+ self.receivedData = nil;
- if (connection == translate_connection)
- {
- [translate_connection release];
- translate_connection = nil;
- } else
- {
- [detect_connection release];
- detect_connection = nil;
+ if (connection == self.translate_connection){
+ self.translate_connection = nil;
+ }
+ else {
+ self.detect_connection = nil;
}
}
Something went wrong with that request. Please try again.