首先让我说我对整个结构和联合事物都很陌生.我在发布这篇文章之前完成了我的作业并诚实地试图获得合法的澄清.如果这是错误的方式请告诉我.首先,我试图创建一个联合,因为我正在组合不同的数据类型.我唯一真正的问题/问题是点运算符vs - >.我可以得到我想用的工作
->
Run Code Online (Sandbox Code Playgroud)
但不是
.
Run Code Online (Sandbox Code Playgroud)
我只是好奇为什么?iv阅读堆栈上的一堆帖子,使用"."显示示例或"答案",但在现实世界中没有运气.我玩过"typedef"就像...
typedef union _myUnion
{
int intValue;
bool boolValue;
}myUnion;
Run Code Online (Sandbox Code Playgroud)
但这并不能解决问题.如果有人解释我做错了什么,将不胜感激.这是我想要做的快速示例.为了简单起见,我将发布一些struct/union语法,并且不会添加任何子类代码,因为这不是问题.
example.h文件
#import "<Foundation/Foundation.h"
union myUnion
{
int intValue;
bool boolValue;
};
@interface MyClass : NSObject
{
union myUnion *someProperty;
}
@property (nonatomic, assign) union myUnion *someProperty;
-(void)doSomething;
@end
Run Code Online (Sandbox Code Playgroud)
example.m
#import "MyClass.h"
@implementation MyClass
@synthesize someProperty = _someProperty;
- (id)init
{
//Some init method...
}
- (void)doSomething
{
NSLog(@"I Did Something...");
}
@end
Run Code Online (Sandbox Code Playgroud)
所以现在在我的其他课上这个有用......
MyClass *newObject = [MyClass alloc] init];
newObject.someProperty->intValue = 6;
Run Code Online (Sandbox Code Playgroud)
但这不...... …
我一直致力于将我的一个应用程序从"共享appdelegate"过程中移开,这个过程看起来很皱眉,尽管它的使用过度.我一直试图为我想做的事情设置协议方法,但我没有运气.我的问题是,你甚至可以让一个viewcontroller将委托请求发送到多个类吗?从我发现它似乎你不能.这是没有意义的,因为我认为这是与mvc的代表和协议的重点.现在只是为了澄清,我知道你可以让一个viewcontroller充当多个其他viewcontrollers的委托.但这不是我要问的.举一个简单的例子,假设你有苹果翻转实用工具模板."完成"按钮只是调用mainvc的委托方法来解除它.现在我们假设我们添加了一个名为...的新类
@interface NewClass : NSObject <TheOtherDelegate>
Run Code Online (Sandbox Code Playgroud)
它有一个委托方法......
- (void)doSomething
{
NSLog(@"The Delegate did something...");
}
Run Code Online (Sandbox Code Playgroud)
我们可以在flipsideviewcontroller上有一个按钮,我们想要调用该委托方法,但是仍然对主视图控制器上的委托方法保持"完成"按钮调用,它会解散它吗?
话虽这么说,我把一个快速的项目放在一起,只是为了看看它是否会起作用,但事实并非如此.我遇到了一个"答案",说你必须首先实例化你想成为代表的班级......
NewClass *myDelegate = [NewClass alloc] init]
[fillInMethodHere setDelegate:myDelegate];
Run Code Online (Sandbox Code Playgroud)
不知道为什么它得到了正确的答案检查,因为不用说它不起作用.有什么我想念的吗?我搜索了ib,看看是否有某个"委托"连接,但我找不到任何东西.
在旁注上,当我在我的工作项目中工作时,我读到了关于删除#import和添加的建议@class.再次,这打破了各种各样的事情.奇怪的是,在此之前,我到目前为止所做的工作和建设都很好.当我删除新的@class和未评论的#import.xcode突然给了我一个错误"无法找到协议减速......"但是,它早了几秒钟才工作.我不得不删除协议代码并重新添加它以使其再次工作.非常痴迷.
任何帮助,将不胜感激.iv在文档,谷歌,堆栈等中读取的所有内容都应该有效,而不是在实际项目中.
我在我的一个类中添加了一个自定义协议,当我尝试在prepareForSegue:方法期间设置委托时,我收到编译器警告.我得到的警告是......
Sending 'MyCustomViewControllerClass *const __strong' to parameter of incompatible type 'id<NSFileManagerDelegate>'
Run Code Online (Sandbox Code Playgroud)
项目构建和运行,一切正常,减去警告.如果我添加<NSFileManagerDelegate>到我的自定义类警告消失.我错过了什么,或者这是Xcode(6 beta)中的错误?代码是设置协议/委托的标准代码,但无论如何我都会发布它...
SomeSecondClass.h
#import <UIKit/UIKit>
@class SomeSecondCustomViewController;
@protocol SomeSecondCustomViewControllerDelegate <NSObject>
- (void)doThisForMe
@end
@interface SomeSecondCustomViewController : UIViewController
@property (weak, nonatomic) id <SomeSecondCustomViewControllerDelegate> delegate;
@end
Run Code Online (Sandbox Code Playgroud)
SomeSecondClass.m
@interface SomeSecondViewController ()
…stuff
-(void)someMethod {
[self.delegate doThisForMe];
}
@end
Run Code Online (Sandbox Code Playgroud)
CustomClass.h
#import <UIKit/UIKit.h>
#import “ SomeSecondViewController.h”
@interface MyCustomViewController : UIViewController <SomeSecondCustomViewControllerDelegate>
//adding on <NSFileManagerDelegate> removes the warning...
@end
Run Code Online (Sandbox Code Playgroud)
CustomClass.h
...standard stuff...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"MySegue"]) {
//this …Run Code Online (Sandbox Code Playgroud) 我一直在环顾四周,并没有找到任何关于这样做的事情,我不确定它是否可能,但这就是我想要实现的目标.请注意,我给出了一个非常简单的例子,说明我正在尝试做什么.此外,发布的代码的相似性都是假设的,并不意味着纠正代码语法.它仅用于通过引用类似的类型功能来帮助进一步说明所需的最终结果.所以在提供替代方法之前请先理解这个概念......
用那个说,正如标题所说,我试图看看我是否可以访问一个对象,让我们通过它的字符串名称或变量来说uilabel 而不使用if语句.例如,假设我们有3个uilabes
UILabel *label1;
UILabel *label2;
UILabel *label3;
Run Code Online (Sandbox Code Playgroud)
从那里我们做了所有适当的合成,什么不是.现在在某些时候我想引用一个基于let say和int的标签.
int intNumber = 2;
Run Code Online (Sandbox Code Playgroud)
这是理论部分.我现在想设置一个标签的文本,该标签的名称中包含匹配的int值.
label(%i, intNumber).text = [NSString stringWithFormat:@"This is Label %i", intValue];
Run Code Online (Sandbox Code Playgroud)
结果将是......
label2.text = @"This is Label 2";
Run Code Online (Sandbox Code Playgroud)
如果int值为1,则将更改label1.或者如果它是3,那么label3会被改变,等等......
或者如果使用标签的字符串名称可以完成相同的过程.
int intValue = 1;
NSString *labelName;
Run Code Online (Sandbox Code Playgroud)
然后在某个时候
labelName = [NSString stringWithFormat:@"label%i",intValue];
Run Code Online (Sandbox Code Playgroud)
现在以某种方式通过引用"labelName"来设置label1的文本来调用它.如果有人能提出建议或评论,甚至可以做到这样的事情,我将不胜感激.我知道你可以通过字符串名称引用类和选择器,但我不确定对象?我不知道"valueForKey:"是否应该是我应该这样做的方式?
对不起啰嗦的帖子感到抱歉,但我只是想确定我说出了我想做的事情.
首先,我要说的是,我已经对此进行了几天的广泛研究,但没有运气。我看过很多例子和类似的情况,例如这个,但到目前为止,没有什么能够解决我的问题。
我的问题是我有一个Python项目,它有一个主类,有两个嵌套类(是的,我知道),其中一个类是第一个类的子类。我不明白为什么我不断得到NameError: global name 'InnerSubClass' is not defined。
我理解范围界定(有问题的两个类都在同一范围内),但我尝试的任何方法似乎都无法解决该问题(我想将两个类的嵌套保持在最低限度),尽管这个问题对其他人也适用。
这是我正在尝试做的一个简单示例:
class SomeClass(object):
def __init__(self):
"""lots of other working stuff"""
class MainClass(object):
def __init__(self):
self.stuff = []
self.moreStuffs = []
class InnerClass(object):
def __init__(self, thing, otherThing):
self.thing = thing
self.otherThing = otherThing
self.otherStuff = []
class InnerSubClass(InnerClass):
def __init__(self, thing, otherThing, newThing):
super(InnerSubClass).__init__(thing, otherThing)
self.newThing = newThing
"""other code that worked before the addition of 'InnerSubClass'"""
def doSomething(self):
innerclass = self.InnerSubClass('thisthing', 'thatthing', 'thingthing')
print("just more thing words …Run Code Online (Sandbox Code Playgroud) 我不确定最有说服力的说法,但我会尽我所能.我创建了一个自定义类,它是一个具有一些属性的通用对象.我创建了几个子类来扩展它并使它们比超类更具体.所以为了举例,我会抛出一些通用的示例代码,这些代码可能是也可能不是正确的语法,只是为了说明我想要完成的事情.
@interface Vehicle : NSObject
@property (nonatomic) int wheels;
- (id)initWithNumberOfWheels:(int)wheels;
@end
Run Code Online (Sandbox Code Playgroud)
从那里我为同一个"汽车"和"卡车"创建一些子类,为课程提供更多细节.
@interface Car : Vehicle
@property (nonatomic) BOOL convertible;
@property etc...
@end
Run Code Online (Sandbox Code Playgroud)
和...
@interface Truck : Vehicle
@property (nonatomic) BOOL is4x4;
@property (nonatomic) int numberOfDoors;
@end
Run Code Online (Sandbox Code Playgroud)
所以这里有趣的地方.我想创建另一个分配这些对象的类,但我希望在init方法中确定车辆的"类型",但使用相同的@property变量.所以例如(再次,这只是给出可视化表示的所有垃圾代码)
Road.h
#import "Car.h"
#import "Truck.h"
@interface Road : NSObject
@property (strong, nonatomic) NotSureWhatToUseHereToMakeThisWork *myRide;
// doesn't work: @property (strong, nonatomic) id myRide;
// doesn't work: @property (strong, nonatomic) Vehicle *myRide;
- (id)initWithCars;
- (id)initWithTrucks;
@end
Run Code Online (Sandbox Code Playgroud)
Road.m
@implementation Road
- (id)initWithCars
{ …Run Code Online (Sandbox Code Playgroud) objective-c ×5
xcode ×2
c ×1
class ×1
cocoa-touch ×1
delegates ×1
ios ×1
protocols ×1
python ×1
python-2.x ×1
xcode6 ×1