如何在 Objective-C 中跨对象实例维护数据?

发布于 2024-08-18 14:56:01 字数 549 浏览 12 评论 0原文

我有一些 iPhone 开发经验,但我完全是自学的,并且正在努力改进我的实践。这可能是一个非常入门的编程问题。

使用对象的所有实例所共有的值来维护实例变量的最佳方法(或者是否可能)是什么?是否可以在不同的地方创建、修改和检查一个对象及其属性?

假设我有一个对象可以跟踪用户滑动屏幕的次数。是否可以在我的 AppDelegate 中分配该对象,然后以某种方式从不同的视图控制器增加该对象的计数变量?

是否可以定义一个实例变量,使其在其所属对象的所有实例之间共享?这样就可以解决问题了。

到目前为止,我刚刚在我的 AppDelegate 中为我需要跟踪的事情创建了 IVAR,然后像这样访问它们:

((MyAppDelegate *)[UIApplication sharedApplication].delegate).instanceVariable

但我不知道这是否是一个好的做法。可能不会。

也许这与全局变量或单例有关。或者上面出现的前缀“共享”,我也在其他地方看到过。我只是在寻找建议或寻找方向。我希望这个问题有意义并且不太笼统。

I've had some experience developing for the iPhone but am entirely self taught and am trying to improve my practices. This is a question that is probably pretty introductory to programming.

What is the best way (or is it possible) to maintain instance variables with values that are common to all instances of an object? Is it possible to have the creation, modification, and checking of an object and its attributes happen in different places?

Say I have an object that keeps track of the number of times a user swipes the screen. Is it possible to, say, allocate that object in my AppDelegate, and then somehow increment the count variable of that object from different view controllers?

Is it possible to define an instance variable such that it is shared across all instances of the object to which it belongs? That would solve the problem.

So far, I've just made IVARs in my AppDelegate for the things like this that I've needed to keep track of and then just accessed them like this:

((MyAppDelegate *)[UIApplication sharedApplication].delegate).instanceVariable

but I don't know if that's a good practice or not. Probably not.

Maybe this is related to global variables or singletons. Or the prefix "shared" that appears above and I've seen in other places too. I'm just looking for advice or a direction to look. I hope this question makes sense and isn't too general.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第七度阳光i 2024-08-25 14:56:01

我认为你所问的问题涉及不同情况的不同做法。在视图控制器之间共享数据是一个有多种解决方案的问题,其中最好的解决方案之一是对所有视图控制器使用委托。

如果您想让一个类管理它自己的所有对象,即使对象来来去去,您也可以执行以下两件事之一:

  1. 使该类具有单例,并跟踪分配和释放以便了解它何时应该创建并自行处置(如果需要)。这不是一个坏方法,尽管它可能会做更多的工作。

  2. 创建一个控制器类来管理其他对象类。一个很好的例子是由视图控制器类控制的视图类。

让我们看看其他人的建议。

I think the question(s) you are asking involve different practices for different situations. To share data among viewcontrollers is one question with a number of solutions, one of the better ones being to use a delegate for all the viewcontrollers.

If you want to have a Class manage all its own objects, even if the objects come and go, you could do one of two things:

  1. Make the Class have a singleton, and keep track of allocations and deallocations in order to know when it should create and then dispose of itself (if needed). This is not a bad way to do it, although it may be more work.

  2. Create a controller class that manages your other class of objects. A good example may be a class of views, controlled by a viewcontroller class.

Let's see what other people suggest.

小猫一只 2024-08-25 14:56:01

马特·加拉格尔 (Matt Gallagher) 发表了一篇关于这个问题的精彩文章。您可以在 Cocoa with Love 中查看它。

然而,正如 Mahboudz 所说,在多视图控制器模式上,委托是首选。

Matt Gallagher had post a great article on exactly that problem. You can see it at Cocoa with Love.

However, on a multiple view controllers patterns as mahboudz said, delegations is preferred.

场罚期间 2024-08-25 14:56:01

在 C++ 或 Java(等)中,您要寻找的是“静态实例变量”,这正是您所描述的:由类的所有实例共享的实例变量。准确地说,Objective-C 没有这样的野兽,但是您可以使用一些普通的 C 语法来提供非常相似的东西。

请参阅本页底部:
http://iosdevelopertips。 com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html

(复制那里的代码,以防页面消失:)

@interface MyClass 
{
  // ...
}
+ (NSString *)myVar;
+ (void)setMyVar:(NSString *)newVa;
@end

@implementation MyClass
static NSString *myVar;
+ (NSString *)myVar { return myVar; }
+ (void)setMyVar:(NSString *)newVar { myVar = newVar; }
@end

在这种情况下,“myVar”是全局的到 .m 文件,但因为它被标记为“静态”(这是一个 C 技巧),所以编译器将其可见性限制为仅其所在的 .m 文件。(与类似的事情一样,请密切关注多线程访问,如果FWIW

,如果您正在构建通常需要健壮的东西,那么每个人共享的类似单例的对象是一个好方法;如果你这样做,你将获得所有正常的 Obj-C 优点(如果你想要的话,内存管理)。

In C++ or Java (etc), what you'd be looking for is a "static instance variable", which is exactly what you describe: an instance variable that is shared by all instances of the class. Objective-C does not have such a beast, precisely, but you can use some normal C syntax to give you something very similar.

See the bottom of this page:
http://iosdevelopertips.com/objective-c/java-developers-guide-to-static-variables-in-objective-c.html

(Copy of the code there in case the page vanishes:)

@interface MyClass 
{
  // ...
}
+ (NSString *)myVar;
+ (void)setMyVar:(NSString *)newVa;
@end

@implementation MyClass
static NSString *myVar;
+ (NSString *)myVar { return myVar; }
+ (void)setMyVar:(NSString *)newVar { myVar = newVar; }
@end

In this case "myVar" is global to the .m file, but because it's marked "static" (this is a C trick), the compiler limits its visibility to only the .m file that it's in. (As with anything like this, keep your eye on multithreaded access if you use that.)

FWIW, if you're building something that generally needs to be robust, a singleton-like object that everybody shares is a good way to go; you get all the normal Obj-C goodness (mem management if you want it) if you do that.

じее 2024-08-25 14:56:01

我会考虑使用单例作为资源管理器 - 也许您不需要资源管理器部分,但单例会对您有很大帮助。

I would look into using a singleton as a resource manager - maybe the resource manager part is not needed for you, but a singleton would greatly help you.

℡寂寞咖啡 2024-08-25 14:56:01

使用将所有内容保留在应用程序委托中并在需要时访问它绝对是不好的做法。至少这意味着代码重用变得很棘手。如果您制作一个新应用程序并想要使用视图或控件或系统的一部分,您还必须确保您的新应用程序委托实现所有正确的选择器和属性。
此外,随着您的应用程序的增长,您将不可避免地发现您的应用程序委托变得更大、更混乱、更难以处理。
调试也变得很痛苦,因为很难准确地找出特定问题涉及哪些代码片段。

我所做的就是勾勒出应用程序的哪些部分实际上需要哪些信息,并将这些信息在创建时传递到 ViewController 中。

It is definitely bad practice to use keep everything in the app delegate and access it where needed.. at the very least it means code reuse becomes tricky. If you make a new app and want to use a view or control or part of the system, you'd have to also make sure your new app delegate implements all the right selectors and properties.
Also, as your app grows you'll inevitably find your app delegate gets bigger and messier and harder to deal with.
Debugging becomes a pain too as it's harder to work out exactly which pieces of code are involved in a particular problem.

What I do this is to sketch out which parts of the app actually need which pieces of information, and pass those into the ViewControllers as they are created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文