我试图设置一个布尔值,但它不会让我设置为true.它一直告诉我YES重新定义.我使用的方法#define YES (q1);,其中q1是布尔.
我正在尝试将NSString转换为NSDate.如果iPhone区域设置为英语(美国),它可以很好地完成,但是当我将它设置为瑞典语时,它不会.
我的代码:
[...]
// Get the date from the post
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZ"];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSDate *dateFromString = [[[NSDate alloc] init] retain];
dateFromString = [dateFormatter dateFromString:[[stories objectAtIndex:storyIndex] objectForKey: @"date"]];
NSLog(@"String: %@", [[stories objectAtIndex:storyIndex] objectForKey: @"date"]);
NSLog(@"date From string: %@", dateFromString);
// Set date string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:MM"];
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
stringFromDate = [stringFromDate stringByReplacingOccurrencesOfString:@"\n" withString:@""]; …Run Code Online (Sandbox Code Playgroud) 我的问题很简单(我想和希望).我想知道当我锁定手机时会发生什么.我做了一个类似时钟的小应用程序,它应该在给定时刻播放和循环声音(比如18:00).但是当我按下电源按钮并且电话锁定时,18:00没有任何反应.当我再次按下电源按钮18:05并显示"锁定屏幕"时,播放声音.
请注意,在锁定手机之前我没有返回主屏幕.当我解锁应用程序时,应用程序仍处于打开状态.有什么办法可以让我的应用程序在我锁定屏幕时仍能完成它的事情吗?或者这只是我可能犯的编程错误?
此致Paul Peelen
我正在使用NSMutableArray符合MKAnnotation协议的对象.我的问题是随着时间的推移,新的对象被添加到数组中,任何人都可以告诉我在mapView上更新注释的首选方法是什么.在添加更新的阵列之前,我是否应该考虑删除所有引脚,或者我是否更好地标记/标记MKAnnotation对象中的现有引脚并仅添加新的(未标记的)引脚?
我正在使用核心数据构建应用程序.由appDelegate加载的我的RootViewController主要来自模板.但是我已将实体名称更改为"Clocks"并添加了一堆行.
我的RootViewController提供了一个带有UINavigationController的MVC.当我将数据保存到我的数据库时,UINavigationController类使用[[UIApplication sharedApplication]delegate]来保存数据以访问我的appDelegate,它实际上预先执行了保存操作.编辑数据的方式相同,但不是在我的app委托中调用insert函数,而是调用update函数.
现在,这一切都很好..实际上完全正确.但是......几次打开编辑并保存在视图中我的应用程序崩溃了.它在模拟器和我的iPhone 4上都是这样做的.这是我的意思的一个例子(电影):http://dl.dropbox.com/u/3077127/has_been_invalidated.mov
这是我的RootViewController.m的代码:
#import "RootViewController.h"
#import "RootViewControllerClockCell.h"
#import "RootViewControllerClockCellFooter.h"
#import "configuration.h"
#import "AddClockViewController.h"
#import "clockAppDelegate.h"
#import "AddClockNavigationController.h"
@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation RootViewController
@synthesize fetchedResultsController=fetchedResultsController_, managedObjectContext=managedObjectContext_;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self setTitle:NSLocalizedString(@"Alarms", @"AddClockNavigationController")];
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAddAlarmView)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; …Run Code Online (Sandbox Code Playgroud) 我收到了错误
*由于未捕获的异常'NSGenericException'终止应用程序,原因:'* Collection <__ NSCFSet:0x6b66390>在枚举时发生了变异.
向我的班级添加新的委托时.或者至少,这就是我认为问题所在.
这是我的代码:MyAppAPI.m
[...]
static NSMutableSet *_delegates = nil;
@implementation MyAppAPI
+ (void)initialize
{
if (self == [MyAppAPI class]) {
_delegates = [[NSMutableSet alloc] init];
}
}
+ (void)addDelegate:(id)delegate
{
[_delegates addObject:delegate];
}
+ (void)removeDelegate:(id)delegate
{
[_delegates removeObject:delegate];
}
[...]
@end
Run Code Online (Sandbox Code Playgroud)
MyAppAPI是一个单例,我可以在整个应用程序中使用它.无论我在哪里(或应该能够)做到:[MyAppAPI addDelegate:self].
这很有效,但仅限于第一个视图.该视图有一个带有PageViewController的UIScrollView,它可以在其自身中加载新视图.这些新视图注册到MyAppAPI以收听消息,直到它们被卸载(在这种情况下,它们执行了操作removeDelegate).但是,在我看来,它在UIScrollView中的第二个视图上执行了addDelegate之后直接死了.
我怎么能改进代码,以免发生这种情况?
更新
我想进一步澄清一下.会发生什么是视图控制器"StartPage"具有带页面控制器的UIScrollView.它加载了几个其他视图(在当前可见屏幕之前1个).每个视图都是一个instans PageViewController,它使用上面显示的addDelegate函数将自身注册到名为MyAppAPI的全局单例.但是,据我所知,当viewcontroller 2注册自己时,这个viewcontroller 1仍在从委托中读取,因此错误显示在上面.
我希望我能清楚地说明这一点.我尝试过一些东西,但没有任何帮助.我需要使用addDelegate向代理注册,即使在从代理中读取时也是如此.我怎么做?
更新2 这是一个响应方法:
+ (void)didRecieveFeaturedItems:(NSArray*)items
{
for (id delegate in _delegates)
{
if ([delegate respondsToSelector:@selector(didRecieveFeaturedItems:)])
[delegate didRecieveFeaturedItems:items];
}
}
Run Code Online (Sandbox Code Playgroud) 我想检查我的NSString是否以@ 2x.png结尾(不区分大小写).我怎样才能做到这一点?
我现在有这个代码,但似乎忽略了@ 2x部分.
NSString *fileName = [_sourcePath lastPathComponent];
[fileName stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
[fileName stringByReplacingOccurrencesOfString:@"@2X" withString:@""];
NSLog(@"Filename: %@", fileName);
Run Code Online (Sandbox Code Playgroud)
2012-01-01 21:00:55.600 NewApp [23930:707]文件名:myfile@2x.png
可能重复:
阻止TOR服务器
我想知道是否有可能看到我的PHP页面的调用是否由TOR客户端完成(意味着调用是通过TOR项目网络进行的).今天我打电话给https://check.torproject.org/cgi-bin/TorBulkExitList.py上的一堆IP地址,但是如果我可以将数据发送回窃取我的API的盗贼那么会更有趣.
无论如何我都想不出来,有可能吗?怎么样?
我有一堆NSNumbers 的数组.从UISlider用户停止拖动时获取某个值.我想从数组中获取关闭数字.
因此,例如,如果用户拖动UISlider到13,并且NSArray包含NSNumberswith 10和15; 我想15从阵列中获取.
数组示例:
NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:15],
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:30],
[NSNumber numberWithInt:45],
[NSNumber numberWithInt:60],
[NSNumber numberWithInt:90],
[NSNumber numberWithInt:110], nil];
Run Code Online (Sandbox Code Playgroud)
如何从阵列中获取正确的数字?
我有一个带有自定义视图的NSMenuItem.但是,当我想更新菜单中的第一行时,它会将内容添加到xib文件中,而不是像我设置的那样.
我的代码:AppDelegate.m:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
_helper = [[Helper alloc] init];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem setMenu:self.statusMenu];
[statusItem setTitle:@"GravAccount"];
[statusItem setHighlightMode:YES];
_gravatarLoader = [[GravatarLoader alloc] initWithTarget:self andHandle:@selector(setGravatarImage:)];
// Get email address
_email = [_helper getEmailAddress];
if (_email != nil)
{
[_gravatarLoader loadEmail:_email withSize:50.0];
}
}
- (void)setGravatarImage:(NSImage*)image
{
NSLog(@"Image loaded!");
GravatarMenuItem *menuItem = [[GravatarMenuItem alloc] initWithNibName:@"GravatarMenuItem" bundle:nil];
[menuItem.label setTitle:_email];
[menuItem.imageView setImage:image];
NSLog(@"Email: %@", _email);
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[item setView:menuItem.view];
[self.statusMenu removeItemAtIndex:0];
[self.statusMenu insertItem:item atIndex:0]; …Run Code Online (Sandbox Code Playgroud) 我有一个 Dictionary 扩展,它在 Xcode 12.5 和 Swift 5.4 发布之前一直运行良好。问题是在 Swift5.4 中似乎发生了变化,这使得可选的可选返回作为误报。在某种程度上它确实有意义,但是现在我的扩展失败了。
考虑以下示例代码:
let dictionary: [String: Any] = ["foo": "bar"]
if let foo = dictionary["foo"] as? String? {
debugPrint("Foo exists")
}
if let bar = dictionary["bar"] as? String? {
debugPrint("bar exists")
}
Run Code Online (Sandbox Code Playgroud)
在 Swift 5.4 之前,这将导致以下结果:
"Foo exists"
Run Code Online (Sandbox Code Playgroud)
但是现在在 Swift 5.4 中,我得到以下结果:
"Foo exists"
"bar exists"
Run Code Online (Sandbox Code Playgroud)
这是因为即使字典被定义为[String: Any],anString?符合类型Any,因此nil符合any,因此 if 情况会触发,因为 nil 是 any。
对我来说,这给我正在使用的 require 函数带来了一个问题,如果泛型类型设置为 optional 就会触发。我的扩展:
"Foo exists"
Run Code Online (Sandbox Code Playgroud)
我的问题:有没有办法检查是否 …
我想从String中创建一个Int,但是找不到怎么做.
这是我的func:
func setAttributesFromDictionary(aDictionary: Dictionary<String, String>) {
self.appId = aDictionary["id"].toInt()
self.title = aDictionary["title"] as String
self.developer = aDictionary["developer"] as String
self.imageUrl = aDictionary["imageUrl"] as String
self.url = aDictionary["url"] as String
self.content = aDictionary["content"] as String
}
Run Code Online (Sandbox Code Playgroud)
使用时toInt()我得到错误消息Could not find member 'toInt'.我也不能用Int(aDictionary["id"]).
objective-c ×8
iphone ×4
ios ×3
cocoa ×2
swift ×2
xcode ×2
boolean ×1
cocoa-touch ×1
core-data ×1
dictionary ×1
dns ×1
enumerate ×1
int ×1
ios4 ×1
iphone-3gs ×1
macos ×1
nsarray ×1
nsmenuitem ×1
nsnumber ×1
nsstring ×1
php ×1
string ×1
swift5.4 ×1
tor ×1