【问题标题】:Xcode objective-C errors need some help :)Xcode Objective-C 错误需要一些帮助:)
【发布时间】:2015-10-20 20:26:12
【问题描述】:

好的,所以我一直在尝试解决这个问题,我已经设法将其归结为这 3 个错误,我一直在寻找解决方法,但没有任何工作可以帮助我解决这个问题吗?

我正在尝试编译一些东西,这是它的一部分,这是我需要修复的所有东西才能让它工作,但我不知道该怎么做。

如果您想查看原始代码,这也不是我的代码,请点击链接致于 Hamzasood

Hamzasood custom watch face

我把错误放在代码旁边,所以寻找

以下是以下文件 1.OnozOmgEditingGuideView.h

//
//  OnozOmgEditingGuideView.h
//  CustomWatchFaceTest
//
//  Created by Hamza Sood on 17/08/2015.
//  Copyright © 2015 Hamza Sood. All rights reserved.


#import <UIKit/UIKit.h>
@class

@interface OnozOmgEditingGuideView : UIView {   <---//expected identifier
    UIView *_topView;
    UILabel *_topLabel;

    UIView *_bottomView;
    UILabel *_bottomLabel;
}
@end
/*!
 Set the color shown by the top view.
    The new color to be displayed
*/
- (void)setTopColor:(UIColor *)color;

/*!
 Set the color shown by the bottom view.
 @param color
    The new color to be displayed
 */
- (void)setBottomColor:(UIColor *)color;   <---//missing content for method declaration

它缺少内容并且正在寻找一些东西,但我不知道我真的对此很陌生。

2.OnozOmgEditingGuideView.m

//
//  OnozOmgEditingGuideView.m
//  CustomWatchFaceTest
//
//  Created by Hamza Sood on 17/08/2015.
//  Copyright © 2015 Hamza Sood. All rights reserved.
//

#import "OnozOmgEditingGuideView.h"

@implementation OnozOmgEditingGuideView {    <---//Expected Method Body
/*
 Initial setup steps:
    1. Create the views and their corresponding labels
    2. Set the label text
    3. Constrain the views
*/
- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {

        // 1
        __strong UIView  **viewsToCreate[]  = { &_topView,       &_bottomView };
        __strong UILabel **labelsToCreate[] = { &_topLabel, &_bottomLabel };
        for (int i = 0; i < sizeof(viewsToCreate)/sizeof(UIView**); i++) {
            UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
            [view setTranslatesAutoresizingMaskIntoConstraints:NO];
            [self addSubview:view];

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
            [label setTranslatesAutoresizingMaskIntoConstraints:NO];
            [view addSubview:label];

             NSLayoutAttribute labelAttributesToConstrain[] = { NSLayoutAttributeCenterX, NSLayoutAttributeCenterY };
            for (int j = 0; j <     sizeof(labelAttributesToConstrain)/sizeof(NSLayoutAttribute); j++) {
                NSLayoutConstraint *constraint = [NSLayoutConstraint   constraintWithItem:label
                                                                                  attribute:labelAttributesToConstrain[j]
                                                                                 relatedBy:NSLayoutRelationEqual
                                                                                     toItem:view
                                                                                attribute:labelAttributesToConstrain[j]
                                                                             multiplier:1
                                                                                  constant:0];
                [constraint setActive:YES];
            }

            *viewsToCreate[i] = view;
            *labelsToCreate[i] = label;
        }

        // 2
        [_topLabel setText:@"First Colour"];
        [_bottomLabel setText:@"Second Colour"];

        // 3
        NSString *constraintsToAdd[] = {
            @"H:|[_topView]|",
            @"H:|[_bottomView]|",
            @"V:|[_topView]-(0)-[_bottomView(==_topView)]|"
        };
        NSDictionary *constraintsViewsDictionary =  NSDictionaryOfVariableBindings(_topView, _bottomView);
        for (int i = 0; i < sizeof(constraintsToAdd)/sizeof(NSString*); i++) {
            [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintsToAdd[i]
                                                                                                options:0
                                                                                             metrics:nil
                                                                                               views:constraintsViewsDictionary]];
        }
    }
    return self;
}
@class
- (void)setTopColor:(UIColor *)color {
    [_topView setBackgroundColor:color];
}

- (void)setBottomColor:(UIColor *)color {
    [_bottomView setBackgroundColor:color];
}

@end

这个给出了预期的方法体

就像我说我不擅长这个所以如果你能帮忙:)

【问题讨论】:

  • 去掉标题顶部的@class

标签: objective-c xcode compiler-errors watchkit apple-watch


【解决方案1】:

在 .h 的顶部,您有 @class 编译器指令,但没有为您正向声明的类提供名称。 @class 用于“前向声明”一个类,它告诉编译器“另一个类 X 将存在,所以不要抱怨还不知道所有细节。”由于您没有提供名称,编译器会感到困惑,因此您会在下一行看到错误。 看到这个问题: Objective-C: @class Directive before @interface?

在 .m 的底部,由于某种原因,您再次有 @class... 但是错误似乎是因为您在@implementation 之后放置了一个开放的大括号{,直到所有方法之后才关闭。那不应该在那里。如果要声明实例变量,请在 @implementation 之后使用花括号。方法不需要。

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2016-02-11
    • 2011-07-23
    • 2012-04-10
    相关资源
    最近更新 更多