我最近一直试图将我的iPhone应用程序的搜索结果存储在NSUserDefaults集合中.我也使用它来成功保存用户注册信息,但由于某种原因,尝试存储我的自定义位置类的NSMutableArray总是返回空.
我尝试将NSMutableArray转换为NSData元素,但是我得到了相同的结果(可以在iPhone上使用NSUserDefaults保存整数数组吗?)
我试过的代码示例是:
保存:
[prefs setObject:results forKey:@"lastResults"];
[prefs synchronize];
Run Code Online (Sandbox Code Playgroud)
要么
NSData *data = [NSData dataWithBytes:&results length:sizeof(results)];
[prefs setObject:data forKey:@"lastResults"];
Run Code Online (Sandbox Code Playgroud)
要么
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:results];
[prefs setObject:data forKey:@"lastResults"];
Run Code Online (Sandbox Code Playgroud)
加载:
lastResults = (NSMutableArray *)[prefs objectForKey:@"lastResults"];
Run Code Online (Sandbox Code Playgroud)
要么
NSData *data = [prefs objectForKey:@"lastResults"];
memcpy(&lastResults, data.bytes, data.length);
Run Code Online (Sandbox Code Playgroud)
要么
NSData *data = [prefs objectForKey:@"lastResults"];
lastResults = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Run Code Online (Sandbox Code Playgroud)
按照下面的建议后,我也在我的对象中实现了NSCoder(忽略过度使用NSString的临时性):
#import "Location.h"
@implementation Location
@synthesize locationId;
@synthesize companyName;
@synthesize addressLine1;
@synthesize addressLine2;
@synthesize city;
@synthesize postcode;
@synthesize telephoneNumber;
@synthesize description;
@synthesize rating;
@synthesize priceGuide; …Run Code Online (Sandbox Code Playgroud) 我一直在尝试为整个NavigationBar(不仅仅是titleView)设置自定义背景,但一直在努力.
我找到了这个帖子
http://discussions.apple.com/thread.jspa?threadID=1649012&tstart=0
但我不确定如何实现给出的代码片段.代码是作为新类实现的吗?另外我在哪里实例化,UINavigationController因为我有一个使用NavigationView模板构建的应用程序,所以它不是在我的根控制器中根据示例完成的
在C#中,我可以使用以下代码来获得仅在调试构建期间执行的代码,如何在Xcode中执行相同的操作?
#if DEBUG
{
// etc etc
}
#endif
Run Code Online (Sandbox Code Playgroud) 我正在创建一些动态linq并遇到以下异常问题:
没有为类型'System.Nullable`1 [System.DateTime]'和'System.DateTime'定义二元运算符GreaterThanOrEqual
我明白了,因为我的字段类型是可以为空的,而且我实际上是在DateTime.Now中传递的.
所以在试图解决这个问题时我已经尝试过了
System.Nullable<DateTime> now;
now = DateTime.Now;
Run Code Online (Sandbox Code Playgroud)
但结果类型是一个不可为空的对象,因此仍然给我上述异常.
有什么建议?!
更新:为了进一步说明,now变量在设置时变为非可空类型,而不是保持为可空的DateTime,因此匹配会引发异常
更新:可以在CodePlex项目中看到实际代码:
http://webquarters.codeplex.com/SourceControl/changeset/view/36529#574700
违规线约为145
fExp = Expression.GreaterThanOrEqual(fExpLeft, fExpRight);
Run Code Online (Sandbox Code Playgroud) 使用以下代码我得到一个很好的格式化字符串:
Request.QueryString.ToString
Run Code Online (Sandbox Code Playgroud)
给我一些像:&hello = worldµsoft =糟透了
但是,当我使用此代码将集合克隆到另一个对象(相同类型)时,我从ToString()方法返回Type().
System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
variables.Remove("sid");
Response.Write(variables.ToString());
Run Code Online (Sandbox Code Playgroud)
是否有更整洁的方式输出它而不是手动查看和构建字符串?
我维护的其中一个网站在很大程度上依赖于使用ViewState(它不是我的代码).但是,在某些ViewState特别容易发生爆炸的页面上,Safari会抛出"Validation of viewstate MAC failed"错误.
这似乎只发生在Safari中.Firefox,IE和Opera都在同一场景中成功加载.
我想在我的应用程序中创建一个自定义联系人列表,提供类似标准联系人列表的AZ跳转列表.
这可能与TableView一起使用吗?
我想声明一个自定义枚举,例如:
enum menuItemType
{
LinkInternal = 0,
LinkExternal = 1,
Image = 2,
Movie = 3,
MapQuery = 4
}
Run Code Online (Sandbox Code Playgroud)
作为我的对象的类型:
@interface MenuItem : NSObject {
NSMutableString *menuId;
NSMutableString *title;
enum menuItemType *menuType;
NSMutableArray *subMenuItems;
}
Run Code Online (Sandbox Code Playgroud)
但我不确定我需要在哪里放置枚举定义 - 如果我把它放在@interface之前它的语法不正确
我一直试着这么做几个小时,这就是我所拥有的
var castItems = typeof(Enumerable).GetMethod("Cast")
.MakeGenericMethod(new Type[] { targetType })
.Invoke(null, new object[] { items });
Run Code Online (Sandbox Code Playgroud)
这让我回头
System.Linq.Enumerable + d__aa`1 [MyObjectType]
而我需要(对于我的ViewData)作为通用列表即
System.Collections.Generic.List`1 [MyObjectType]
任何指针都会很棒
我使用以下代码(来自博客文章)来调整图像大小
if (inImage.size.width <= inImage.size.height) {
// Portrait
ratio = inImage.size.height / inImage.size.width;
resizedRect = CGRectMake(0, 0, width, width * ratio);
}
else {
// Landscape
ratio = inImage.size.width / inImage.size.height;
resizedRect = CGRectMake(0, 0, height * ratio, height);
}
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
resizedRect.size.width, // width
resizedRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * resizedRect.size.width, // rowbytes …Run Code Online (Sandbox Code Playgroud) c# ×4
iphone ×4
objective-c ×4
.net ×2
asp.net ×1
c#-3.0 ×1
casting ×1
cocoa-touch ×1
collections ×1
generics ×1
query-string ×1
reflection ×1
safari ×1
uitableview ×1
viewstate ×1
xcode ×1