Hot answers tagged

32

The problem here isn't that your Swift looks like Objective-C. The problem is that, because you're developing a toy app, you've used a toy design, and now you don't like the toy design. Probably you followed a tutorial you found on the Internet (or even in Apple's docs), and most of those are for toy apps and toy designs. In those tutorials, the author ...


25

You could simplify the above significantly: class FGSingleton { static let sharedInstance = FGSingleton() var gameScore: Int = 0 // METHODS private init() { println(__FUNCTION__) } func displayGameScore() { println("\(__FUNCTION__) \(self.gameScore)") } func incrementGameScore(scoreInc: Int) { self....


21

According to the iOS Device Compatibility Reference, the iPhone 3GS was the only iOS 6-capable device to lack a front camera. The same document states that your application can declare the requirement for a front-facing camera by setting the UIRequiredDeviceCapabilities key such that front-facing-camera is true. That way, you can safely strip out the ...


21

The current range of iOS devices that support 7.0+ don't include any devices that lack a front-facing camera. I also agree with you that it is unlikely that Apple will release any future devices without the front-facing camera (though not impossible). Even if they do, however, you could and should specify that your app requires this hardware in the ...


20

Very regularly when dealing with asynchronous stuff (among other things) in iOS development, we have to make a decision between three tools. Closures (blocks, callbacks, etc) NSNotificationCenter The delegate pattern. Which one is right for you depends on the scenario. It won't always be the same one. As a general rule, we can use the number of objects ...


18

First and foremost, printing text to the console is absolutely pointless for an iOS application. It's okay to do it as an easy way to test whether our code is working as intended, but we don't want to mistakenly leave this in the final release build. So, step 1: Follow the instructions in this StackOverflow post to set up a custom compiler flag for your ...


16

In this case, your indentation makes the code drift over to the right so does make the code difficult to read so does need some changes. First I would note that a lot of code is repeated and also as noted a switch after converting the condition to an int is possible. However in this case your code is effectively doing multiple lookups so I would look at ...


16

The first thing that stands out to me is that you've created an entirely new class that exists purely to have this class method. This doesn't make a whole lot of sense. At a minimum, this method could just exist on its own, outside of a class, but I'll do you one better: let's write it as an extension to UIDevice, that's where it makes the most sense, and ...


15

for(int i = 1; i <= 19; i++) { i = ( i%2 == 0 && i!=19 ) ? i+1 : i; [waitFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"lev_wait2%04d.png", i]]]; } Holy Toledo. First of all, if you must update the iterator within a for loop, let's leave the update statement empty: ...


14

In GitHub for Mac, we use a combination of AFNetworking and our own Mantle and ReactiveCocoa frameworks for API requests. AFNetworking makes it trivial to create your own "network manager" type object as a subclass of AFHTTPClient. We enqueue requests on it, ask them to hand back a certain kind of MTLModel subclass, and then subscribe to the resulting ...


13

Because this question is so, so large, I won't get into all the specifics of everything I see. I will point out some things, and provide some examples from one file or another, and as you work through my answer, you should work through your project to find all the other instances of an example I point out. I will start with a simple answer to the topic ...


13

I'm just a beginner myself but I feel like I can point out a few things in this code. First, I would add some white space at the top of the file here: #import "ESFlashingErrorBar.h" #import "ESThemeManager.h" @implementation ESFlashingErrorBar #define kNumOfPoints 7 int lastFlash; Instead I would do this for increased readability: #import "...


13

Lately, when writing Code Review posts, I like to point out the first thing I notice when I look at someone code. This is the first thing I notice when I look at your code. Without looking at anything else, I can already tell you that your code is way too deeply nested and therefore absolutely confusing. With looking at your code, I can tell you that not ...


12

I observe that: Only one function has any comments There is a fair amount of commented out code The variable names aren't very helpful Somethings are just misspelled: getSyrvey, rectoreWorm The code seems to mix various things together (drawing/particle system/database access) There is also this nugget: for(int i = 1; i <= 19; i++) { i = ( i%...


11

You're right. There is a simpler way. First of all, instead of giving every interactive element its own method, let's give them all the same method. - (IBAction)buttonPressed:(id)sender; If they're not all buttons, a different method name is in line, but for this example, I'll assume them all to be buttons. Now then, whether you're creating these UI ...


11

Scope You should reduce the scope of variables to the minimum needed. So, if possible make them private. Something like public float x,y; public float getX() { return x; } public float getY() { return y; } should be avoided because it removes encapsulation. Sometimes it is ok to expose a variable to the public, but you should usually ...


11

func backgroundExecutable(f: (() -> Void) -> Void) -> (() -> Void) This is madness and it takes a number of mental gymnastics to figure out what is going on here. This method takes, as its only (and poorly named) argument, a closure which returns void and takes, as its single argument, a closure which returns void from taking no arguments. ...


10

In iOS 4 and later you are encouraged to use animation blocks. [UIView animateWithDuration: 0.5f delay: 0.08f options: UIViewAnimationCurveEaseIn animations: ^{ self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); } completion: ^(BOOL finished)...


10

Instance Variables and Properties Understanding exactly what a @property is is absolutely crucial to being a good Objective-C programmer. Here's my crash course. An instance variable is just that. An instance variable. It's pretty straight forward. It's just a variable in that can be accessed by all the methods in your class. And if it's a public ...


10

public static float difficulty; I don't really see why this is static. You only have one MainScreen instance in your game. int GAMESTATE; This is not a static final variable so it should not be named with ALL_CAPS. gameState is a better name. @Heslasher is correct that you are using a lot of magic numbers in your code, especially when it comes to this ...


9

The correct way to check for success or failure of Cocoa (Touch) methods is documented in "Handling Error Objects Returned From Methods" in the "Error Handling Programming Guide": Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are ...


9

The problem with any sort of timer in which each "tick" is a measurement from the previous "tick" is that in software, these "ticks" are not guaranteed to be exact. This means that if your first tick is off by a tenth of a second, every remaining tick will be off by that amount or more. Your second tick is scheduled based on your first tick. This means ...


9

If you need to check if a certain method exists you can also check if the object supports it via respondsToSelector: like this: if ([object respondsToSelector:@selector(openURL:options:completionHandler:)]) { [object openURL:...options:...completionHandler:...]; } else { [object openURL:...]; } I think this approach is more flexible than checking ...


8

Put your image mapping in a property list: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>113</key> <dict> <key>condition</key> <string>Sun.png</string> ...


8

Winston's answer points out some improvements to your algorithm. There are other ways in which we can slightly improve the speed of this. In Objective-C, for loops (and while and do...while) loops are handled one iteration at a time and the exit condition is checked on each iteration of the loop. Meanwhile, a forin loop is handled in batches. When we're ...


8

When you have something like this in your code: - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } You can simply delete all 5 of these lines. The only reason to include the the stub for - (void)didReceiveMemoryWarning is if you're actually going to add code to the method. int ...


8

Now, given that basically all Objective-C is written to make use of Cocoa frameworks, and we're talking about programs to run on OSX or iOS, we have to discuss Apple. Apple is pretty consistent when their method naming conventions, and their way of doing things. So any conversation about Objective-C standard practice would be incomplete without a very ...


8

A slightly better option that takes a little more effort up front but saves a lot of time in the long run, and makes the code more readable (and the obfuscated word harder to crack) would be to follow this pattern... First, create an NSString class category and fill that category with a readonly property for every character. @interface NSString (Obfuscater)...


8

@property (retain) id chessdelegate; There are several things wrong with this line. retain - While this technically still is a property attribute, it has been replaced by strong. It does the exact same thing as strong, but strong is the vastly preferred term here. retain or strong are both wrong here anyway. Any delegate property should ALWAYS be weak or ...


Only top voted, non community-wiki answers of a minimum length are eligible