Basics Method Override - iphone

First of all, Sorry. I'm not good at English.
Hi. I'm beginner of iOS.
To learn Objective-c for the first time
Define the class name and method name in the header file and declare the class object created above in the main.m file on implementation details. M calling the method does.
[NewPoint SetPoint: 3];
[NewPoint print];
But iOS studied, I suddenly was wondering iOS studied.
Without calling the method directly from the main.m, appdelegate.m method override you if you do not like this [NewPoint SetPoint:3] that is the exact reason why I wonder.
Why Running just override method not using [NewPoint SetPoint:3] like this.

For learning purpose it was taught to create class in main.m itself.
But in real programming world, you follow classes which are a blue print, so these are created separately for full use throughout the application and other classes/objects are able to use them.
Classes make your code separate from different classes. AppDelegate is one such class which comes initially with a project.
Later on you will make your own classes like Person, Employee, Box etc each one having their own properties, behaviours etc.
What I feel is in your training you were taught to create a class and object as NewPoint, later it was created in AppDelegate. Now you can try to use a differenct class called NewPoint and make an object of that inside AppDelegate and use it.

Related

Using Objective-C category to avoid compilation error

I did something a little bit nasty today.
All of our view controllers inherit from two different parent view controllers, let's say XXXViewController and YYYViewController. XXXViewController in turn inherits from TrackedUIViewController, which is a class provided in the Google Analytics SDK so all your view controllers can inherit from it and easily track them.
YYYViewController however, inherits from a different type of view controller. Ah, and it's an open sourced piece of code that I really don't want to change.
What's the problem here? We cannot track any of the YYYViewController children because we can't access the methods provided in TrackedViewController, since they are private.
I don't want to modify the source provided in the Google Analytics SDK. So what did I do? Create a category that exposes those methods, just to avoid the compilation error.
The obvious downside to this is that the GA source code changes it may break, but it will be fairly easy to detect.
I was wondering what other problems I could be facing by doing this, and if you guys can think of a better approach.
Thank you
You can go up in the inheritance chain of YYYViewController, see in turn what class it inherits from. If it's UIViewController, simply change that particular superclass to TrackedUIViewController in the source and you're fine to go.
Example for better understanding: suppose YYYViewController inherits from ZZZViewController, which in turn inherits from UIViewController. Now you can change the superclass of ZZZViewController from UIViewController to TrackedUIViewController - since TrackedUIViewController inherits from UIViewController, no functionality will be lost, but magically your whole YYYViewController class will become trackable.
Hope this helps :-)
You already mentioned the biggest risk of using undocumented APIs: Changes to the API are beyond your control and may break your logic, for example if methods are removed/renamed, or their behavior stops matching your expectations.
From the purely technical point of view, I can see no further problems, since in Objective-C all methods are public. As long as they continue to exist, you may continue to call them.
I would say the root problem is excessive subclassing. Keep view controller hierarchies shallow. Use composition instead of subclassing. If you must subclass ensure that functionality that a class provides can be turned on & off by it's subclasses.

Why we use app delegate in our application

I am new in iphone development and i needed a array which i use to access in different class so my senior told me that you should declare in App delegate and access it another class in which it require, so i wrote line for that
MyAppAppDelegate * appObject =
(MyAppAppDelegate*)[[UIApplication sharedApplication]delegate];
I done my task successfully but i couldn't get it 100%. Someone tell that why we use this and in what exactly situation in which we've to use this?
AppDelegate is loaded first when you run your application as it contains window. So, the variable and objects you want to access throughout your project is declared in AppDelegate. You just have to create an instance and you can access all the objects in AppDelegate.
ApplicationDelegate can be useful as a singleton class but you have to use it with discretion - and there are varying opinions on this - but if you have a few global type properties or methods you want to recall from various other classes, and I emphasize few, then ApplicationDelegate may be a nice place to add these.
And yes, it is bad design - but you can get away with it if you are prudent and as #Sedate Alien mentions, take a look at dependency injection.
The purpose of ApplicationDelegate, by the way, is mainly to handle events like loading your application, when you return to home screen, when you come back from home screen, etc.

How do I share a method between two classes?

I have an iPhone app that uses a Tab Bar Controller with 3 tabs. Each tab is a separate class. There are several methods that are identical in each class. Instead of having three copies of the same method, I'd like to share the method between the classes. However, I have not figured out how to do this.
Thanks.
This is a classical case of inheritance. Create a base class, and put all things that are common across the classes you are trying to build, into it, both functions and data members. Then, derive your three classes for your tabs from this class (inherit from it, or make it the parent class, lots of overlapping terms here that people generally throw around). Make sure your methods in the parent class are NOT defined as private methods. That would make them inaccessible to your child classes. Hope that helps!
Here you can make a new class and define that method inside that class which you want to share between more than one class.
Now whenever you want to access that method, just import the class and you can use the same method in multiple classes.
Let me know if you need more help.

Best practice for View-Routing in iPhone SDK

I've run into a little problem while developing a Core Data driven Quiz and be a bit confused about a best practice to solve my problem.
I have approximately five templates for the different questions, which will be loaded in case which question is displayed. So I check which template has question 1 and push the new question-template view into my navigation controller. Because its always the same code I want to write a function (I came from php) which gets the next question-id as argument and decide which template will be loaded and push the next view into the navigation-controller.
What is the best practice to solve this problem? Can I write a function with access to the navigation-controller, and my Core Data classes. And if yes where I have to create this function?
Okay I think I found an way but gets here another Error. I create a class called QuestionRouter and define an class method. I'll import this class into each viewController where it needed. The class method gets the correct template from Core Data without an problem. But now I wan't to load the correct view. For that I need to access the navigationController defined in my AppDelegate.
So how I can access the navigationController in my AppDelegate for another class?
Hope for an answer.
Mister-D

Is it possible to share code betwee classes that does not have the same parent?

I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share some common code between them, say for example, code that handles an action after a popup dialog etc. How do I do this?
Refactor the shared code into its own class, and include an instance of that class as a field/property within your other two classes.
You can re-factor the appropriate code into a utilities class, and then have the two classes call it. As for the iPhoneSDK, you can probably have the utility method be the delegate method itself.
You could write a category on a common ancestor class. Then both classes could import that Category and call the common code.