在iPhone应用程序中访问Web服务是我没有找到一个清晰,美观的解决方案的问题.我不是在谈论如何发送查询或解析响应,而是关于"大图"的答案.
忽略服务器端技术,您如何将Model对象插入Web服务?你如何设计你的代理对象?你如何缓存资源?
说我有类方法
+ (double)function:(id)param1 :(id)param2
{
// I want to memoize this like...
static NSMutableDictionary* cache = nil;
//
// test if (param1,param2) is in cache and return cached value, etc. etc
//
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!
这是我想要做的..
我有一个名为userInfo的类.我在另一个名为LoginInfo的类中创建了此对象的实例.我想让这个实例保持活跃并且可以访问所有其他类,直到应用程序还活着...
我如何实现这一目标?我在某处读到了我可以用单例类做到这一点.但我不知道它们是什么......我对可可很新.请指导..
提前致谢..
@interface UserInfo : NSObject {
NSString * firstName;
NSString * lastName;
NSString * uID;
NSString * password;
NSString * userType;
}
-(id)initWithFirstName:(NSString *)fname andLastName:(NSString *)lname andUID:(NSString *)userID andPassword:(NSString *)pwd andUserType:(NSString *)type;
@property (readwrite, copy) NSString * firstName;
@property (readwrite, copy) NSString * lastName;
@property (readwrite, copy) NSString * uID;
@property (readwrite, copy) NSString * password;
@property (readwrite, copy) NSString * userType;
@end
#import "UserInfo.h"
@implementation UserInfo
-(id)initWithFirstName:(NSString *)fname andLastName:(NSString *)lname andUID:(NSString *)usid andPassword:(NSString *)pwd andUserType:(NSString *)type{ …Run Code Online (Sandbox Code Playgroud) 这是一个经典问题.
我想从我的应用程序中的任何位置访问一组对象.我也想用单身人士做这件事.我的问题是:
所有代码和示例都非常感谢!
编辑1
这就是我到目前为止所拥有的.我无法弄清楚如何正确和一致地访问这个香蕉数组:
#import <Foundation/Foundation.h>
@interface Singleton : NSObject {
NSMutableArray *bananas;
}
@property (nonatomic, retain) NSMutableArray *bananas;
@end
#import "Singleton.h"
static Singleton *mySingleton;
@implementation Singleton
@synthesize bananas;
#pragma mark SingletonDescption stuff
+ (Singleton *)mySingleton
{
if(!mySingleton){
mySingleton = [[Singleton alloc]init];
}
return mySingleton;
}
+ (id)allocWithZone:(NSZone *)zone
{
if (!mySingleton) {
mySingleton = [super allocWithZone:zone];
return mySingleton;
} else {
return nil;
}
}
- (id)copyWithZone:(NSZone*) zone
{
return self;
}
- (void)release
{ …Run Code Online (Sandbox Code Playgroud) 最直接和最简单的实现是这样的
static MySingleton *_instance = nil;
+ (MySingleton *) instance
{
@synchronized (_instance)
{
if (_instance == nil)
{
_instance = [[MySingleton alloc] init];
}
return _instance;
}
}
Run Code Online (Sandbox Code Playgroud)
实际上我知道几个关于单身的热门帖子,比如 在iOS中实现Singleton 和pop 模板
所以我的问题是"上述实施的任何缺陷"?
我是iOS编程的新手,我对单例类是什么以及使用它的原因很感兴趣.我找到了一些信息,但它含糊不清.特别是我想将它应用于实例.我的项目使用Facebook SDK,我想为我的NSDictionary创建包含好友列表的单例类.我的.m委托文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//here is some other code
facebook = [[Facebook alloc] initWithAppId:@"my app id" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_location",
@"friends_location",
@"read_friendlists",
nil];
[facebook authorize:permissions];
[permissions release];
}
[facebook requestWithGraphPath:@"me/friends" andDelegate:(id)self];
//here is some other code
}
Run Code Online (Sandbox Code Playgroud)
我设置了我的NSDictionary值请求返回朋友列表:
- (void)request:(FBRequest *)request didLoad:(id)result {
_friendsDictionary = result;
} …Run Code Online (Sandbox Code Playgroud) *我肯定需要休息......原因很简单 - 数组没有分配...感谢您的帮助.由于这个令人尴尬的错误,我标记了我的帖子以删除它.我觉得它对用户没用;)*
我刚刚尝试在iOS中创建一个单例类,但我可能犯了一个错误.代码(无需ARC):
#import "PeopleDatabase.h"
#import "Person.h"
#import <Foundation/Foundation.h>
@interface PeopleDatabase : NSObject{objetive
NSMutableArray* _arrayOfPeople;
}
+(PeopleDatabase *) getInstance;
@property (nonatomic, retain) NSMutableArray* arrayOfPeople;
@end
Run Code Online (Sandbox Code Playgroud)
-
@implementation PeopleDatabase
@synthesize arrayOfPeople = _arrayOfPeople;
static PeopleDatabase* instance = nil;
-(id)init{
if(self = [super init]) {
Person* person = [[[Person alloc] initWithName:@"John" sname:@"Derovsky" descr:@"Some kind of description" iconName:@"johnphoto.png" title:Prof] retain];
[_arrayOfPeople addObject:person];
NSLog(@"array count = %d", [_arrayOfPeople count]); // <== array count = 0
[person release];
}
return self;
}
+(PeopleDatabase …Run Code Online (Sandbox Code Playgroud) 我担心这是一个愚蠢的问题,但我老实说不明白.
我有一个类,RemoteConfiguration目前它的功能是所有实例方法.既然如此,我必须使用它:
RemoteConfiguration* configs = [[RemoteConfiguration alloc] init];
[configs updateSettings];
[configs release];
Run Code Online (Sandbox Code Playgroud)
这很烦人,因为没有必要创建一个对象只是为了使用一个方法,它可以是一个类方法,并调用如下:
[RemoteConfiguration updateSettings];
Run Code Online (Sandbox Code Playgroud)
但是,当我将我的方法从Xcode 更改-为+抱怨我访问的每个对象时self,建议我使用->.(这也会引起警告)我不明白这一点.无论方法如何,对象仍将具有其成员变量.那么为什么有->必要呢?如何在类方法中使用成员变量?
我在方法viewDidLoad中创建了一个对象:在我创建的另一个方法中,我想访问此对象.
一种方法是在h文件中声明对象.
另一种方法是将其作为参数传递.
还有其他方法吗?
问候,
我正在尝试编写我的第一个iPhone应用程序.我需要能够访问所有视图中的数据.用户登录时存储数据,此后需要对所有视图可用.
我想创建一个静态类,但是当我尝试访问静态类时,我的应用程序在控制台上没有输出时崩溃了.
是将数据写入文件的唯一方法吗?或者是否有其他我没有想到的清洁解决方案?
提前谢谢了,
分析状态泄漏问题,为什么?
+ (DebugOutput *) sharedDebug
{
@synchronized(self)
{
if (sharedDebugInstance == nil)
{
[[self alloc] init];
}
}
return sharedDebugInstance;
}
Run Code Online (Sandbox Code Playgroud) objective-c ×10
iphone ×5
singleton ×5
ios ×4
cocoa-touch ×3
c ×2
c++ ×1
class ×1
class-method ×1
cocoa ×1
facebook ×1
memoization ×1
methods ×1
object ×1
sharing ×1
static ×1
web-services ×1
xcode ×1