From 86dd0e99bc210f2bdbcd0c4407ec1d7b6074b001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Wed, 27 May 2015 12:51:30 -0700 Subject: [PATCH] Defailablize init methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If an OpenGL context couldn’t be created, there’s just no point to soldiering on. Things are going to fall apart pretty quickly anyways. Assert and be done with it. --- include/mbgl/ios/MGLMapView.h | 4 ++-- platform/ios/MGLMapView.mm | 39 ++++++++++------------------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h index c7bb8cf1f..0ccb8225b 100644 --- a/include/mbgl/ios/MGLMapView.h +++ b/include/mbgl/ios/MGLMapView.h @@ -22,14 +22,14 @@ IB_DESIGNABLE /** Initializes and returns a newly allocated map view with the specified frame and the default style. * @param frame The frame for the view, measured in points. -* @return An initialized map view or `nil` if the map view couldn’t be created. */ +* @return An initialized map view. */ - (instancetype)initWithFrame:(CGRect)frame; - (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken __attribute__((unavailable("Use -initWithFrame:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:]."))); /** Initializes and returns a newly allocated map view with the specified frame and style URL. * @param frame The frame for the view, measured in points. * @param styleURL The map style URL to use. Can be either an HTTP/HTTPS URL or a Mapbox map ID style URL (`mapbox://`). -* @return An initialized map view or `nil` if the map view couldn’t be created. */ +* @return An initialized map view. */ - (instancetype)initWithFrame:(CGRect)frame styleURL:(NSURL *)styleURL; - (instancetype)initWithFrame:(CGRect)frame accessToken:(NSString *)accessToken styleURL:(NSURL *)styleURL __attribute__((unavailable("Use -initWithFrame:styleURL:. Set MGLMapboxAccessToken in the Info.plist or call +[MGLAccountManager setAccessToken:]."))); diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index a959bce7b..a517a4d26 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -114,41 +114,32 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) - (instancetype)initWithFrame:(CGRect)frame { - self = [super initWithFrame:frame]; - - if (self && [self commonInit]) + if (self = [super initWithFrame:frame]) { + [self commonInit]; self.styleURL = nil; - return self; } - - return nil; + return self; } - (instancetype)initWithFrame:(CGRect)frame styleURL:(NSURL *)styleURL { - self = [super initWithFrame:frame]; - - if (self && [self commonInit]) + if (self = [super initWithFrame:frame]) { + [self commonInit]; self.styleURL = styleURL; - return self; } - - return nil; + return self; } - (instancetype)initWithCoder:(NSCoder *)decoder { - self = [super initWithCoder:decoder]; - - if (self && [self commonInit]) + if (self = [super initWithCoder:decoder]) { + [self commonInit]; self.styleURL = nil; - return self; } - - return nil; + return self; } - (NSString *)accessToken @@ -193,20 +184,14 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) _mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]); } -- (BOOL)commonInit +- (void)commonInit { _isTargetingInterfaceBuilder = NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent; // create context // _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; - - if ( ! _context) - { - mbgl::Log::Error(mbgl::Event::Setup, "failed to create OpenGL ES context"); - - return NO; - } + NSAssert(_context, @"Failed to create OpenGL ES context."); // setup accessibility // @@ -394,8 +379,6 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration) MGLEventKeyZoomLevel: @(zoom), MGLEventKeyPushEnabled: @([MGLMapboxEvents checkPushEnabled]) }]; - - return YES; } -(void)reachabilityChanged:(NSNotification*)notification