ios - 导入一类函数头

标签 ios methods import

我正在重构我的代码以使其更易于管理,我想创建一个包含可以加载到其他类中的函数的类。

我创建了一个名为函数的类,将 funtions.h 导入到我的 ViewController 类的 .h 中,将函数 .m 导入到 ViewController.m 中,但编译器在调用时无法识别方法 hasInternetconnection 并崩溃。

我并没有完全迷失为什么我不能在这个类中调用这个方法

这是我的代码,我已经通过 s/o 和 google 仔细查看了,但我仍然看不出我做错了什么

函数.h

#import <Foundation/Foundation.h>

@interface Functions : NSObject


-(BOOL)hasInternetConection;
@end

函数.m

#import "Functions.h"

@implementation Functions



-(BOOL)hasInternetConection{
    NSURL *url=[NSURL URLWithString:@"www.google.com"];
    NSURLRequest *req=[NSMutableURLRequest requestWithURL:url];
    NSHTTPURLResponse *res=nil;
    [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:NULL];
    if (res!=nil) {
        return NO;
    }else{
        return YES;}

}

@end

HomeViewController.h

...
#import <QuartzCore/QuartzCore.h>
#import "multiShotViewController.h"
#import "Functions.h"
...

@interface HomeViewController : UIViewController {

UIGlossyButton *b;

HomeViewController.m

...
#import "detailsViewController.h"
#import "Functions.h"
#define  Kwelome @"welcomeread"


@interface HomeViewController ()

@end

@class Functions;
@implementation HomeViewController
@synthesize tripName;
@synthesize databasePath, deathtrail;
@synthesize lampingbtn,deerstalkingbtn,boundarybtn, optionsbtn,shootingbtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationItem.title = @"Home";
        UIColor *backg=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bgcamo.png"]];
        self.view.backgroundColor=backg;
        [backg release];
    }
    return self;
}
...

最佳答案

我认为@class函数;至少不需要。您已经导入了头文件,因此无需重新声明它。

你在哪里调用这些方法?您确定在该类的实例上调用它们吗?

我怀疑您正在尝试解决一个问题

[Functions hasInternetConection]

代替

Functions * func = [[Functions alloc] init];
[func hasInternetConection];
[func release];

如果您像第一个示例中那样执行操作,则将函数中的声明更改为“+”而不是“-” - 这样它就可以用作静态方法。

关于ios - 导入一类函数头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729883/

相关文章:

c# - NSNetService 使用 C# 分配 IP 地址

ios - 在 iOS 中读取文本文件

php - 如何在 Woocommerce 项目中显示变体名称

C# 方法的最大值?即尺寸(int x < 40,int y < 80)?

javascript - 我应该绑定(bind)内置的 React 方法吗?

node.js - 具有NodeJS/CommonJS样式模块系统的语言

import - Rails 3.1 Assets 管道不接受对@import'd 表的更改

ios - MKAnnotation 未在 iOS5 中被选中

import - 如何加载实现相同行为的多个模块

ios - 使用 NSStringCompareOptions 在 iOS 中对负数字符串进行排序