相关疑难解决方法(0)

在cocoa objective-c类中变量前面的下划线如何工作?

我在一些iPhone示例中看到,属性在变量前面使用了下划线_.有谁知道这意味着什么?或者它是如何工作的?

我正在使用的接口文件如下所示:

@interface MissionCell : UITableViewCell {
    Mission *_mission;
    UILabel *_missionName;
}

@property (nonatomic, retain) UILabel *missionName;

- (Mission *)mission;
Run Code Online (Sandbox Code Playgroud)

我不确定上面做了什么,但是当我尝试设置任务名称时:

aMission.missionName = missionName;
Run Code Online (Sandbox Code Playgroud)

我收到错误:

请求成员'missionName'的东西不是结构或联合

variables syntax cocoa cocoa-touch objective-c

156
推荐指数
4
解决办法
5万
查看次数

_和自我之间的区别.在Objective-C中

使用下划线并self在调用时使用Objective-C中的关键字是否有区别@property

财产申报:

@property (weak, nonatomic) NSString *myString;
Run Code Online (Sandbox Code Playgroud)

致电@synthesize酒店:

@synthesize myString = _myString;
Run Code Online (Sandbox Code Playgroud)

如果我想在我的代码中使用它,是否有区别?什么时候?在getter/setter中?

self.myString = @"test";
_myString = @"test";
Run Code Online (Sandbox Code Playgroud)

objective-c getter-setter ios synthesize

48
推荐指数
2
解决办法
2万
查看次数

合成属性和带下划线前缀的变量:这是什么意思?

可能重复:
属性名称上的下划线前缀?

这是什么意思?@synthesize window=_window;我知道一般来说这意味着'某个类'有一个窗口,但为什么要使用_window而不仅仅是window?这是名称空间吗?

cocoa-touch properties objective-c ios

19
推荐指数
1
解决办法
1万
查看次数

您的Application Delegate设置在哪里以及谁初始化其window和viewController属性?

我有一个关于IOS应用程序的新手问题...如果我创建一个名为TestForStackOverflow的新的基于视图的应用程序,Xcode会自动为TestForStackOverflowAppDelegate.h创建这样的代码:

@class TestForStackOverflowViewController;

@interface TestForStackOverflowAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet TestForStackOverflowViewController *viewController;

@end
Run Code Online (Sandbox Code Playgroud)

以及TestForStackOverflowAppDelegate.m中的以下内容:

#import "TestForStackOverflowAppDelegate.h"

#import "TestForStackOverflowViewController.h"

@implementation TestForStackOverflowAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

[...]
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

1)TestForStackOverflowAppDelegate类在哪里设置为当前应用程序的委托?它是"自动"完成的吗?我已经看到main.m源文件只包含以下代码:

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil); …
Run Code Online (Sandbox Code Playgroud)

xcode objective-c uiapplicationdelegate ios

15
推荐指数
1
解决办法
1万
查看次数

self.variableName与_variableName对@sysnthesize variableName

可能重复:
cocoa objective-c类中变量前面的下划线如何工作?


注意:对于那些试图理解这一点的人来说,我想出了我的困惑的根源.在.h,我有:

...
@interface myClass : parentClass {
className *variableName:
}

@property (strong, nonatomic) className  *variableName;
...
Run Code Online (Sandbox Code Playgroud)

这导致self.variableName和_variableName是.m中的两个不同变量.我需要的是:

...
@interface myClass : parentClass {
className *_variableName:
}

@property (strong, nonatomic) className  *variableName;
...
Run Code Online (Sandbox Code Playgroud)

然后,在类'.m中,self.variableName和_variableName是等价的


在全新的Xcode的4.5+,与ARC,针对iOS 5.0及项目,有一个明显的优势(运行时的效率,速度等)使用_variableNameself.variableName与旧式@synthesize variableName

我的理解是Xcode 4.5+将创建一个_variableName相当于的默认访问器,self.variableName并且唯一不使用的原因@synthesize variableName是为了避免iVars和传入变量之间的混淆,对吗?

对我来说,只是使用self.variableName访问iVar似乎是最直接和明确的,你正在寻找哪个变量.除了打字_self.,使用是否有优势_variableName

objective-c synthesize

8
推荐指数
2
解决办法
9289
查看次数

Objective-C:在命名变量中使用_(下划线)

可能重复:
在Objective C中使用下划线前缀属性名称

在Objective-C的书中我正在阅读,在我看到的一些代码中,有时人们会在变量名称中添加下划线.

虽然我意识到这是由于一个既定的惯例,我想知道:

下划线是先于还是完成变量名称是否有任何意义?例如,_name, name and name_作为Objective-C程序员,如果有什么,下划线对您有意义吗?

objective-c naming-conventions

7
推荐指数
2
解决办法
8531
查看次数

Objective-C中的属性声明

可能重复:
cocoa objective-c类中变量前面的下划线如何工作?

所以我发现在合成属性时你必须使用下划线,这对我来说没有任何意义.

那么,让我们开始吧.在我们的.h文件中,我们写了这一行:

@property (nonatomic) double speed;
Run Code Online (Sandbox Code Playgroud)

在我们的.m文件中,我们这样做:

@synthesize speed = _speed;
Run Code Online (Sandbox Code Playgroud)

为什么?据我所知,property生成一个实例变量并为它创建setter和getter.但到底是怎么回事

@synthesize speed = _speed 
Run Code Online (Sandbox Code Playgroud)

做?常识告诉我,我们将_speed中的值赋予速度.好的.我们在哪里宣布_speed?为什么编译器没有给我们错误?它应该是什么意思?为什么这样混淆代码?

我的问题在这里:

如果我这样做会发生什么

@synthesize speed;
Run Code Online (Sandbox Code Playgroud)

没有_speed,我会得到错误或一些错误吗?这种语法后的原因是什么?制作时他们在想什么?_speed来自哪里?它是什么?它是指针还是真正的价值?到底是怎么回事?

objective-c ios

4
推荐指数
1
解决办法
1万
查看次数

Objective-C:_variable

好吧,以前一定要问这个但是我看起来很生气,一无所获:

我在我的iphone应用程序中有一个简单的数组,我这样定义:

@property (nonatomic, strong) NSArray *pages;
@synthesize pages = _pages;
Run Code Online (Sandbox Code Playgroud)

我在Apples示例代码中看到了这一点,并且认为这是编写self.pages(即_pages替换self.pages)的一个很好的捷径,如下所示:

_pages = [[NSArray alloc] init];
Run Code Online (Sandbox Code Playgroud)

但是Apple再次拥有它(不完全像这样,但看起来好像他们一直随机交换):

self.pages = [NSKeyedUnarchiver unarchiveObjectWithData:contents];
Run Code Online (Sandbox Code Playgroud)

最后:

[_pages release];
Run Code Online (Sandbox Code Playgroud)

这完全让我感到困惑._pages和self.pages之间会有什么区别?

谢谢你的帮助.

iphone global-variables objective-c self ios

1
推荐指数
1
解决办法
1336
查看次数

iOS自定义委托从viewController调用setDelegate后返回Nil

我在ARC中使用自定义委托类时遇到了一个奇怪的问题.在viewDidLad之后的viewcontroller中,我调用工具栏setDelegate方法并将viewcontroller指定为委托.

每当我尝试引用委托时,它都是零,即使在viewcontroller中设置之后也是如此.

//MPToolbar.h

#import <UIKit/UIKit.h>

//Define the protocol for the delegate
@class MPToolBar;
@protocol MPToolBarDelegate <NSObject>
@required
- (void)writeButtonSelected;
- (void)writeButtonDeSelected;
@end

@interface MPToolBar : UIView{
    id <MPToolBarDelegate> delegate;
}

@property(strong) UIButton *lastButton;

@property(strong) id <MPToolBarDelegate> delegate;
-(IBAction)buttonSelected:(id)sender;
-(void)resizeForOrientation:(UIInterfaceOrientation)orientation;

@end

//MPToolbar.m
#import "MPToolBar.h"

@implementation MPToolBar
@synthesize lastButton, delegate;

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"MPToolBar" owner:self options:nil];
    [self addSubview:[nibViews objectAtIndex:0]];
    return self;
}


#pragma button actions
-(IBAction)buttonSelected:(id)sender{
    UIButton …
Run Code Online (Sandbox Code Playgroud)

ios

1
推荐指数
1
解决办法
3462
查看次数